I am reading 10 lines from a simple RandomAccessFile and printing each line. The exact lines in the text file are as follows one after the other:
blue
green
bubble
cheese
bagel
zipper
candy
whale
monkey
chart
When printing them in order as I read line by line my output is this:
green
cheese
zipper
whale
chart
I cannot understand why my method is skipping every other line in the file. Am I misunderstanding how a RandomAccessFile works? My method is below:
RandomAccessFile file = new RandomAccessFile(FILEPATH, "rw");
read(file);
public static void read(RandomAccessFile t) throws IOException{
while (t.readLine()!=null) {
System.out.println(t.readLine());
}
}
You are calling readLine()
twice
while (t.readLine()!=null) {
System.out.println(t.readLine());
}
Instead precompute the readLine()
;
String tmp;
while((tmp = t.readLine()) != null) {
System.out.println(tmp);
}