Re: convenient way to read text file multiple times without reopening it




"Tomas Mikula" <tomas.mikula@xxxxxxxxx> wrote in message
news:pan.2007.11.15.00.26.29.851166@xxxxxxxxxxxx
Hi,

I suppose that a convenient way to read text file is through FileReader.
But FileReader does not support the reset().
So, as a temporal remedy, I am opening the file twice:

Reader fr = new FileReader("filename");
.....
fr.close();
.....
fr = new FileReader("filename");
.....


Of course this is not a clean way. At least, the file could possibly be
removed or renamed by another program between the two calls to "new
FileReader()". So I want to open the file once and then read it
(sequentially) twice.
I suppose Java provides a convenient way for doing it, but I'm unable to
figure it out.

Thank you for pointing me the right way!

On some OS's there's no guarantee the file will be locked while you are
reading it so if you are seriously concerned about preventing the file from
changing you'll have to know more about how your OS handles that problem.
Java NIO includes file locking, but how it works is up to the OS.

Presuming, however, that if you open a file for reading it will naturally be
locked for writing, you can always solve your problem by doing the following
so that the overlapping read locks block out write access.

Reader reader1 = new FileReader ("filename");

// Read as much as you like

Reader reader2 = new FileReader ("filename");
reader1.close ();

// Read it again

reader2.close ();


It may also be possible (I haven't tried this but it's a fairly easy test)
to open your file as a FileInputStream, get the stream's FileChannel and
then reset the position of that channel.

Matt Humphrey http://www.iviz.com/


.



Relevant Pages

  • Re: File I/O problem
    ... no unusual characters in the filename). ... from a text file I'd have thought FileReader was better - the API says ... "FileInputStream is meant for reading streams of raw bytes such as ... When I printed folder in my for loop it always did ...
    (comp.lang.java.programmer)
  • Re: File I/O problem
    ... no unusual characters in the filename). ... from a text file I'd have thought FileReader was better - the API says ... "FileInputStream is meant for reading streams of raw bytes such as ...
    (comp.lang.java.programmer)
  • Re: Adding Contents of a Vector
    ... Vector tokens = new Vector; ... // Start reading the file ... FileReader fr = new FileReader; ... BufferedReader inFile = new BufferedReader; ...
    (comp.lang.java.programmer)
  • Re: convenient way to read text file multiple times without reopening it
    ... I suppose that a convenient way to read text file is through FileReader. ... But FileReader does not support the reset. ... So, as a temporal remedy, I am opening the file twice: ...
    (comp.lang.java.programmer)
  • convenient way to read text file multiple times without reopening it
    ... I suppose that a convenient way to read text file is through FileReader. ... But FileReader does not support the reset. ... So, as a temporal remedy, I am opening the file twice: ...
    (comp.lang.java.programmer)