• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I change the current position in Stream??  RSS feed

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to change the current line number of my Reader stream. For that I have used LineNumberReader.setLineNumber() but it is not working.

Can any one please tell me is there any way to do that?

Thanks & Regards
Bikash
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got reset() (which can go back to the beginning) and skip() (which can go from the current position to some later position.) If a Reader supports both of those, you can get to any point in the data. The problem is that not all Readers support them.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using BufferReader for reading a file and this Reader supports
skip().But skip() needs the number of characters needs to be skipped.How I can get that?

My situation is:

I am reading a file line by line and not writting it anywhere and checking condition and if condition not match I should skip next line.

Can you please guide me how I can do that ?

Thanks & Regards
Bikash
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to take a look at RandomAccessFile. I don't know if this will solve your problem, especially if you are reading text a line at a time. It is difficult to know how long a line of text is without reading it in first. Random access files are usually more helpful with binary data where you can specify the size of each "record" in the data. Then you can use seek() to skip a specified number of records by doing some simple arithmetic.

With that said, why do you need to move the position of the file pointer in the stream? Would it be easier to read the whole file into a String, (using StringBuffer or StringBuilder, of course) and then move around inside the String to the information you want? Of course, this is infeasible for very large files, but if your file is relatively small so that you can fit it all into memory, I think this would be a decent solution. Since I don't know the details of your project, I am only guessing here. If you would like to provide more information, perhaps we can suggest alternative solutions.

Keep Coding!

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!