Java BufferedReader back to the top of a text file?

The Buffered readers are meant to read a file sequentially. What you are looking for is the, and then you can use seek() to take you to where you want in the file.

The random access reader is implemented like so:

     String fileName = "c:/myraffile.txt";
     File file = new File(fileName);
     RandomAccessFile raf = new RandomAccessFile(file, "rw");
} catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
} catch (IOException e) {
     // TODO Auto-generated catch block

The "rw" is a mode character which is detailed here.

The reason the sequential access readers are setup like this is so that they can implement their buffers and that things can not be changed beneath their feet. For example the file reader that is given to the buffered reader should only be operated on by that buffered reader. If there was another location that could affect it you could have inconsistent operation as one reader advanced its position in the file reader while the other wanted it to remain the same now you use the other reader and it is in an undetermined location.

Leave a Comment