Solved

Getting Only the End Content of a File in Java

Posted on 2006-06-25
9
202 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
Apps blocked by Java 9 88
JavaFX TableView not displaying correctly 3 58
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

828 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