I want to be able to read four lines from a txt file each time a button is pressed until there are no more lines to be read. When I press the button the first time, the first four lines are read. The problem is that when I press the button the second time it won't read the next four lines. The txt file looks like this
one
two
three
four
five
six
seven
eight
should I not be creating a BufferedReader every time I press the button?
public void fnext() {
try {
BufferedReader br = new BufferedReader(new FileReader(fr));
if(!br.ready()) {
br.close();
JOptionPane.showMessageDialog(null, "No more data");
}
text = br.readLine() + " ";
text += br.readLine() + " ";
text += br.readLine() + " ";
text += br.readLine() + " ";
br.readLine();
p.setText(text);
} catch (IOException ex) {
Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
Declare BufferedReader outside this function.
should I not be creating a BufferedReader every time I press the button? Yes, if you create the object every time you click a button, it will read top 4 lines every time.
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.