Home arrow static arrow Java Programming [Archive] - File operations
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - File operations
This topic has 6 replies on 1 page.

Posts:56
Registered: 5/7/04
File operations  
Jul 5, 2004 4:44 AM



 
Hello,
I have a small problem I am reading a text file using buffered reader and at some point in between I need to go one backwards does any one know how to acces the line which I already processed.
Thankyou
Regards
Sreedhar
 

Posts:47
Registered: 6/22/01
Re: File operations  
Jul 5, 2004 5:49 AM (reply 1 of 6)



 
I think, the only thing you could do is to read the entire file first into a string, divide it into lines using the StringTokenizer, fill the lines into an ArrayList and then navigate over the entries:

Example:

String file = readFile(); // Read the entire file here into a Stringjava.util.StringTokenizer st = new java.util.StringTokenizer(file, "\n"); // replace "\n" with the appropriate linefeedjava.util.List lines = new java.util.ArrayList();while(st.hasMoreTokens()) {    lines.add(st.nextToken());}String[] linesArray = lines.toArray(new String[lines.size()); System.out.println(linesArray);
 

Posts:5,904
Registered: 04/03/99
Re: File operations  
Jul 5, 2004 5:59 AM (reply 2 of 6)



 
What a very odd suggestion. BufferedReader has a readLine method.

The easiest way to do this is simply to retain a reference to the "last" line, which you set at the end of processing each line. If you determine that you need to re-process the old data, you have it to hand to do so. If you need something a bit more complex, you could either read it all into memory first (just add each line to a List as you grab it from the BufferedReader) then process the List instead of the file, or use the mark and reset methods (see the JavaDoc for details).

If you need any more help, it would be useful to know why you need to "go back" a line.

Dave.
 

Posts:47
Registered: 6/22/01
Re: File operations  
Jul 5, 2004 6:05 AM (reply 3 of 6)



 
Indeed. Can I remove my post somehow?
 

Posts:5,904
Registered: 04/03/99
Re: File operations  
Jul 5, 2004 6:19 AM (reply 4 of 6)



 

No, but we all make mistakes. All mine can be found by searching the archives. Don't worry about it.

Dave.
 

Posts:56
Registered: 5/7/04
Re: File operations  
Jul 5, 2004 6:53 AM (reply 5 of 6)



 
I think you all have mistaken my question but any way thanks for your replies coz I found the solution. I wanted to set the pointer or the reference back to the previous line which I proccessed not the data and I did it by using file "mark" and then "reset" it to same point. Its very easy.
Regards
Sreedhar
 

Posts:5,904
Registered: 04/03/99
Re: File operations  
Jul 5, 2004 7:01 AM (reply 6 of 6)



 

Which part of my suggestion to use "mark and reset" was confusing ?

They're not guaranteed to be supported unless you know the type of the underlying Reader that the operation requires. That's presumably the case here, but you didn't actually say that.

Dave.
 
This topic has 6 replies on 1 page.