6 Replies - 256 Views - Last Post: 10 October 2012 - 01:08 PM Rate Topic: -----

#1 liftthis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 08-May 05

would like some ideas how to parse the last 40 bytes from file

Posted 08 October 2012 - 05:58 PM

I need some help I am trying to parse a file for the last 40 bytes of data.

My file could be x number of bytes long. I only want the last 40 bytes of data to save as string, and then I want to replace the file getting ride of the 40 bytes of data.

public String removeHdr(File file1, File replaced) 
{
    	long sizeOfFile =0;

    	sizeOfFile = file1.length();

	System.out.println("file size " + sizeOfFile );

	return null;
    	
 }




Is This A Good Question/Topic? 0
  • +

Replies To: would like some ideas how to parse the last 40 bytes from file

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: would like some ideas how to parse the last 40 bytes from file

Posted 08 October 2012 - 10:21 PM

Save the read chars/bytes in a ByteBuffer(havent tried using this before) so you can rewrite the file without the last 40 bytes
My example below does only get the last 40 bytes, it does not save the leading bytes
Instead of skipping (in my example) you should read the bytes :) Just keep track of how many you need to read and it should be no problem

long offset = file.lengt() - 40; //We only want to read the last 40 bytes

if (offset > 0) {
	FileInputStream reader = new FileInputStream("myfile.txt");
	
	//Skip until there are just 40 bytes left in the stream
	long remainingSkips = offset; //How many bytes we still have to skip
	while (remainingSkips != 0) 
		remainingSkips -= reader.skip(remainingSkips);
	
	//We should now have 40 bytes left
	byte[] buffer = new byte[40];
	reader.read(buffer);
	return new String(buffer);
}


Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: would like some ideas how to parse the last 40 bytes from file

Posted 08 October 2012 - 11:11 PM

I wouldn't use a loop.

public static void main(String[] args) {
        FileInputStream fis = null;
        File file = new File("files/file.txt");
        byte[] b = new byte[40];

        try {
            fis = new FileInputStream(file);
            
            long skipSize = file.length() - (file.length() - 40); 
            
            fis.skip(skipSize);
            
            fis.read(B)/>;
            
            String s = new String(B)/>;
            
            System.out.println(s);
            
        } 
        catch (IOException ex) { 
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }//end main



This is a blocking operation that should be done off your main Thread.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: would like some ideas how to parse the last 40 bytes from file

Posted 09 October 2012 - 12:24 AM

View Postfarrell2k, on 09 October 2012 - 06:11 AM, said:

I wouldn't use a loop.

public static void main(String[] args) {
        FileInputStream fis = null;
        File file = new File("files/file.txt");
        byte[] b = new byte[40];

        try {
            fis = new FileInputStream(file);
            
            long skipSize = file.length() - (file.length() - 40); 
            
            fis.skip(skipSize);
            
            fis.read(B)/>;
            
            String s = new String(B)/>;
            
            System.out.println(s);
            
        } 
        catch (IOException ex) { 
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }//end main



This is a blocking operation that should be done off your main Thread.


You need the loop if you read how skip works

Skip does not guarantee to skip the bytes you tell it to, instead it returns how many bytes it was able to skip. That is why you need the loop
I agree that it should work like in your example, but sadly it does not and it has caused problems for many programmers ^^
Was This Post Helpful? 1
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: would like some ideas how to parse the last 40 bytes from file

Posted 09 October 2012 - 01:10 AM

Good catch :)
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: would like some ideas how to parse the last 40 bytes from file

Posted 10 October 2012 - 04:32 AM

A RandomAccessFile is perfect for this job:
    public static String tailAndTruncate(String filePath, int tailSize) {
        /**
         * Get the last 'tailSize' bytes from a file as a String
         * and remove that from the original file
         */
        String result = null;
        RandomAccessFile raf = null;

        try {
            raf = new RandomAccessFile(filePath, "rw");
            raf.seek(raf.length() - tailSize);

            byte[] tailBytes = new byte[tailSize];
            raf.read(tailBytes);
            result = new String(tailBytes);
            raf.setLength(raf.length() - tailSize);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                raf.close();
            } catch (IOException e) { /* ignore */
            }
        }

        return result;
    }


Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: would like some ideas how to parse the last 40 bytes from file

Posted 10 October 2012 - 01:08 PM

using file.length() you can skip the number of bytes - 40
or open the file as a RandomFile and reads only strarting from the end - 40

Sorry g00se I missed your post
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1