简体   繁体   中英

How do I read a file again using buffered reader in Java?

I have a Java code that reads through an input file using a buffer reader until the readLine() method returns null . I need to use the contents of the file again indefinite number of times. How can I read this file from beginning again?

You can close and reopen it again. Another option: if it is not too large, put its content into, say, a List.

Buffer reader supports reset() to a position of buffered data only. But this cant goto the begin of file (suppose that file larger than buffer).
Solutions:
1.Reopen
2.Use RandomAccessFile

A single Reader should be used once to read the file. If you want to read the file again, create a new Reader based on it.

Using Guava 's IO utilities, you can create a nice abstraction that lets you read the file as many times as you want using Files.newReaderSupplier(File, Charset) . This gives you an InputSupplier<InputStreamReader> that you can retrieve a new Reader from by calling getInput() at any time.

Even better, Guava has many utility methods that make use of InputSupplier s directly... this saves you from having to worry about closing the supplied Reader yourself. The CharStreams class contains most of the text-related IO utilities. A simple example:

public void doSomeStuff(InputSupplier<? extends Reader> readerSupplier) throws IOException {
  boolean needToDoMoreStuff = true;
  while (needToDoMoreStuff) {
    // this handles creating, reading, and closing the Reader!
    List<String> lines = CharStreams.readLines(readerSupplier);
    // do some stuff with the lines you read
  }
}

Given a File , you could call this method like:

File file = ...;
doSomeStuff(Files.newReaderSupplier(file, Charsets.UTF_8)); // or whatever charset

If you want to do some processing for each line without reading every line into memory first, you could alternatively use the readLines overload that takes a LineProcessor .

I faced with the same issue and came wandering to this question.

1. Using mark() and reset() methods:

BufferedReader can be created using a FileReader and also a FileInputStream. FileReader doesn't support Mark and Reset methods. I got an exception while I tried to do this. Even when I tried with FileInputStream I wasn't able to do it because my file was large (even your's is I guess). If the file length is larger than the buffer then mark and reset methods won't work neither with FileReader not with FileInputStream. More on this in this answer by @jtahlborn.

2. Closing and reopening the file

When I closed and reopened the file and created a new BufferedReader, it worked well. The ideal way I guess is to reopen the file again and construct a new BufferedReader as a FileReader or FileInputStream should be used only once to read the file.

try {
            BufferedReader br = new BufferedReader(new FileReader(input));

            while ((line = br.readLine()) != null)
            {
                //do somethng
            }
            br.close();
        }
        catch(IOException e)
        {
            System.err.println("Error: " + e.getMessage());

        }
}

If you want to do this, you may want to consider a random access file . With that you can explicitly set the position back to the beginning and start reading again from there.

i would suggestion usings commons libraries

http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

i think there is a call to just read the file into a byteArray which might be an alternate approach

Not sure if you have considered the mark() and reset() methods on the BufferedReader

that can be an option if your files are only a few MBs in size and you can set the mark at the beginning of the file and keep reset()ing once you hit the end of the file. It also appears that subsequent reads on the same file will be served entirely from the buffer without having to go to the disk.

you do this by calling the run() function recursively, after checking to see if no more lines can be read - here's a sample

// Reload the file when you reach the end (i.e. when you can't read anymore strings)
  if ((sCurrentLine = br.readLine()) == null) {
    run();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM