Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can I change the current position in Stream??

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic