Solved

Getting Only the End Content of a File in Java

Posted on 2006-06-25
9
205 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 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

688 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