Getting Only the End Content of a File in Java

Hi everyone,

I was wondering if it was possible to get only the last 10K of a file content instead of reading the entire file to save memory.  Can anyone point me to the right direction, or maybe share some basic code that would do this?  That would be so cool.

Thank you so much,


Jazon from Fort Myers, FL
LVL 3
piratepatrolAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
Here you go

            public static byte[] tail(File f, int length) {
                  RandomAccessFile raf = null;
                  byte[] result = null;
                  try {
                        raf = new RandomAccessFile(f, "r");
                        result = new byte[length];
                        int pointer = (int)f.length();
                        raf.seek(f.length() - length);
                    raf.read(result);
                  }
                  catch(Exception e) {
                    e.printStackTrace();
                  }
                  finally {
                        if (raf != null) {
                              try { raf.close(); } catch(IOException e) { e.printStackTrace(); }
                        }
                  }
                  return result;
            }
0
 
CEHJCommented:
You can use a RandomAccessFile. Seek to the end of the file and then seek and read backwards
0
 
StillUnAwareCommented:
You can use RandomAccessFile from java.io
Using that Object You can open a file and move the file pointer whereever You want using the seek(long l) function.
See:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
StillUnAwareConnect With a Mentor Commented:
It would look something like that:

    RandomAccessFile raf = new RandomAccessFile("filename", "rw");
    byte[] buf = new byte[raf.length() > 10000 ? 10000 : (int)raf.length()];
    raf.seek(raf.length()-buf.length);
    raf.read(buf);
    System.out.println(new String(buf));
    raf.close();
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Is that somebody's homework done ;-) ?
0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
0
 
CEHJCommented:
>> Is that somebody's homework done ;-) ?

Wouldn't have thought so - not a very homeworky question ;-)

>>CEHJ, you want to speak on Stringbuffers ;-) ?

Have done ;-)

0
 
Mayank SAssociate Director - Product EngineeringCommented:
Thanks :)
0
 
piratepatrolAuthor Commented:
You're all so wonderful!  Thank you so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.