Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to read the same file multiple times without having to re-open it over

800808Dec 7 2011 — edited Dec 9 2011
Hi All,

I am wondering whether there is a way to read a text file multiple times without having to re-open it over and over again on Windows platform.

Below is how I have done it by re-opening it which may not be the most efficient method:
FileReader localEmpFrIn1st = new FileReader(new File("C:\Employee.txt"));
BufferedReader localEmpBrIn1st = new BufferedReader(localEmpFrIn1st);
while ((strLine1st = localEmpBrIn1st.readLine()) != null) {
     .............
}   
                
System.out.flush();         // line 287
localEmpBrIn1st.close();  // line 288
localEmpFrIn1st.close();  // line 289
                
FileReader localEmpFrIn2nd = new FileReader(localEmpFIn);
BufferedReader localEmpBrIn2nd = new BufferedReader(localEmpFrIn2nd);
               
while ((strLine2nd = localEmpBrIn2nd.readLine()) != null) {
    .........
}
Using either localEmpFrIn1st.mark(0) or localEmpFrIn1st.reset() resulted in the following error:

SEVERE: null
java.io.IOException: mark(0) or reset() not supported
* at java.io.Reader.mark(Reader.java:232)*
* at processEmployee(processEmployee.java:289)*
Any reason why this exception is occurring and how to avoid it? I believe that another way to use mark() & reset() effectively is with the use of RandomAccessFile but don’t know how to read C:\Employee.txt in sequence, the same as how FileReader would in this example illustrated.

I thought the purpose of line 287 was to flush all data in cache so that it would be ready for mark(0) or reset() again.

Thanks in advance,

Jack
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2012
Added on Dec 7 2011
5 comments
2,160 views