Solved

Getting Only the End Content of a File in Java

Posted on 2006-06-25
9
199 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:piratepatrol
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16979705
You can use a RandomAccessFile. Seek to the end of the file and then seek and read backwards
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16979712
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
 
LVL 14

Assisted Solution

by:StillUnAware
StillUnAware earned 230 total points
ID: 16979728
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 230 total points
ID: 16979775
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16981216
Is that somebody's homework done ;-) ?
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 40 total points
ID: 16981219
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16981639
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16981691
Thanks :)
0
 
LVL 3

Author Comment

by:piratepatrol
ID: 17006438
You're all so wonderful!  Thank you so much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now