Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting Only the End Content of a File in Java

Posted on 2006-06-25
9
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 920 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
CEHJ earned 920 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 160 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

636 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