1

I have found out that FileReader scans the file only once. After that you have to close it and re-initialize it to re-scan the file if you want in a program. I have read about this in other blogs and stackoverflow questions, but most of them mentioned BufferedReader or some other type of readers. The problem is that I have already finished my program using FileReader and I don't want to change everything to BufferedReader, so is there anyway to reset the file pointer without bringing in any other classes or methods? or is there anyway to just wrap a BufferedReader around my already existing FileReader? Here is a small code that I wrote specifically for this question and if I can wrap a BufferedReader around my FileReader, I want you to do it with this code snippet.

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Files {
    public static void main(String args[]) throws IOException{
        File f = new File("input.txt");
        FileReader fr = new FileReader(f);
        int ch;
        while((ch = fr.read()) != -1){
         // I am just exhausting the file pointer to go to EOF
        }
        while((ch = fr.read()) != -1){
        /*Since fr has been exhausted, it's unable to re-read the file now and hence 
        my output is empty*/
            System.out.print((char) ch);
        }
    }
}

Thanks.

4
  • A BufferedReader will not really solve the problem. You would have to make the buffer so big that the whole file is kept in memory. What's so bad about reopening the file? Commented Apr 22, 2016 at 7:41
  • Oh I see. I have no problem with re-opening, in fact, I may need to reopen it only 1-2 times in my program, but I just wanted to avoid redundant code and was hoping if I could find any answers here. That re-opening thing is kind of my last resort in case I don't get any answer here or in case I end up having trouble implementing the answers given here. Thanks Commented Apr 22, 2016 at 8:06
  • You could try reset(). There is no hard guarantee however on cross-platform compatibility. Commented Apr 22, 2016 at 8:11
  • @JoopEggen It's not working on my machine though, I guess that's the platform compatibility problem. It says java.io.IOException: reset() not supported. Commented Apr 22, 2016 at 8:24

1 Answer 1

2

Use java.io.RandomAccessFile like this:

    RandomAccessFile f = new RandomAccessFile("input.txt","r"); // r=read-only
    int ch;
    while ((ch = f.read()) != -1) {
        // read once
    }

    f.seek(0); // seek to beginning

    while ((ch = f.read()) != -1) {
        // read again
    }

EIDT ------------
BufferedReader also works:

    BufferedReader br = new BufferedReader(new FileReader("input.txt"));
    br.mark(1000); // mark a position

    int ch;
    if ((ch = br.read()) != -1) {
        // read once
    }

    br.reset(); // reset to the last mark

    if ((ch = br.read()) != -1) {
        // read again
    }

But you should be cafeful when using mark():
The mark method in BufferedReader: public void mark(int readAheadLimit) throws IOException. Here is it's usage copied from javadoc :

Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, this is simpler and effective. So RandomAccessFile is same as FileReader except that it has a seek method? or are there any pros and cons in using this against a FileReader?
@rohitkrishna094 They are totally different things. FileReader extendsInputStreamReader, it only handles the FileInputStream. RandomAccessFile implements both DataOutput and DataInput, support both reading and writing to a random access file. Suggest you to google RandomAccessFile to learn more by yourself.
Yeah I just checked it out. Thanks for the prompt reply that helped my with my program quickly. :D
@rohitkrishna094 As someone said reset also works, not FileReader but BufferedReader, i just updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.