• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read the same ascii file multiple times without having to re-opening it

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am wondering whether there is a way to read a text file multiple times without having to re-open it again on Windows platform.
Below is how I have done it by re-opening it which is not the efficient method:

There are some suggestions around that require C:\Employee.txt to be defined as RandomAccessFile before a
combination of mark() / reset() could be used. However, I am not familiar with how to use RandomAccessFile
to read the file in sequence such as the above example. Alternatively, neither does mark() nor reset() exising in
FileReader class.

Thanks in advance,

Jack
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Bush wrote:I am wondering whether there is a way to read a text file multiple times without having to re-open it again on Windows platform.
Below is how I have done it by re-opening it which is not the efficient method:
...
There are some suggestions around that require C:\Employee.txt to be defined as RandomAccessFile before a
combination of mark() / reset() could be used. However, I am not familiar with how to use RandomAccessFile
to read the file in sequence such as the above example. Alternatively, neither does mark() nor reset() exising in
FileReader class.


Several points:
1. reset() DOES exist in the FileReader class, because it's inherited from java.io.Reader.

2. While it's true that I/O operations such as open and close are slower than native Java code, the chances of you even noticing it are likely to involve hundreds of those operations.
Unless you have a clear and provable need to optimize, code simplicity and correctness is MUCH more important than a few milliseconds of time.

3. RandomAccessFile and FileReader are NOT interchangeable, as they don't share any common ancestry or interfaces.

4. The fastest way of all to re-process data is to read the whole thing into an array or List and iterate that as many times as you need.

Lesson: Don't complicate your life by worrying about optimization that you'll probably never need. Concentrate on writing clear, correct programs.

You may also want to look at the quote at the bottom of my post.

Winston
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:You may also want to look at the quote at the bottom of my post.


You seem to have unchecked the "Append Signature" checkbox for this particular post, so the sig isn't actually shown here...

Update: Hm, mine doesn't show either - there may be default mechanism at work here if that checkbox is turned off for the first post in a topic.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You seem to have unchecked the "Append Signature" checkbox for this particular post, so the sig isn't actually shown here...


Actually I didn't. It just doesn't seem to have been added for this thread (it's fine on my other posts today).

If you're interested, it's:

"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason...including blind stupidity." — W.A. Wulf

and it's my favourite quote about PO.

Winston
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

reset() DOES exist in the FileReader class, because it's inherited from java.io.Reader.



The following I/O error was generated when trying use FileReader.reset() on the same file after having completed reading it from the first while loop:

SEVERE: null
java.io.IOException: reset() not supported


I understand where you are coming from but my purpose was to make the code as concise and simple as possible, by eliminating any unnecessary duplicate where possible. Performance is not an issue in this case.

Thanks,

Jack
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Bush wrote:...my purpose was to make the code as concise and simple as possible...Performance is not an issue in this case.


In which case, I suspect that the simplest and most concise code is to open and close the file as many times as you need to. Alternatively, try and think of a way to do what you want without scanning it multiple times; however, since we have no idea what it is you're trying to do, iit's difficult to make any suggestions on that front.

PS: The exception you're getting indicates that the File itself does not support resetting; not that the method doesn't exist. Now that may have to do with the way you're opening it, or the way you've wrapped the FileReader; but if it was me: I'd take it as a sign from God that there's a better way to do this.

Winston
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston for your clarifications and advice on best practice in programming. Its good to know that I am already doing the right thing.

Jack
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic