• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File pointer

 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a file which i need to read frist n line and last n number of line only. So is there is any API methods which allow to jump from one line to another with out reading and read only line number which i am interested.
Thanks in advance.
Regards
arun
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The closest to what you're looking for in the standard API is
java.io.LineNumberReader.
You can start from there. It has a method readLine() which returns a String. I don't know exactly what you mean by "without reading it".
You may decorate the LineNumberReader interface and add skipLine, getNumberOfLines, readLine(int lineNumber) etc ...
W.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for input Wilfried.

I thought that setLineNumber() will help to solve my problem. But it is not. Might be my
understanding is wrong.
I wrote a simple test method with the following line of code

File oFile= new File("src.txt");
LineNumberReader oLineNoReader = new LineNumberReader(new FileReader(oFile));
oLineNoReader .setLineNumber(3);
System.out.println( "content of 3rd line : " + oLineNoReader.readLine() );

Assume src.txt content is as follows

line 1
line 2
line 3
line 4
...
...

I WANT THE OUTPUT AS FOLLOWS
content of 3rd line : line 3
BUT STILL I AM GETTING OUT PUT AS
content of 3rd line : line 1
Any suggestion please.
Thanks again.
Arun
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A BufferedReader would also allow you to read one line at a time.
Perhaps RandomAccessFile is more what you'd like to use.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic