aspose file tools*
The moose likes Java in General and the fly likes How to read the same ascii file multiple times without having to re-opening it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read the same ascii file multiple times without having to re-opening it" Watch "How to read the same ascii file multiple times without having to re-opening it" New topic
Author

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

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Oct 20, 2006
Posts: 235
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Oct 20, 2006
Posts: 235
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.
 
subject: How to read the same ascii file multiple times without having to re-opening it