So I have a class and what it does is read the contents of a file and then does something based on the contents.
package maple;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
public class SetSettings {
public SetSettings() {
BufferedReader reader;
File f = new File (Shared.saveLocation + "Settings.txt");
String line = "";
if (f.exists()) {
while (line != null) {
try {
//System.out.println("COMES HERE");
reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
line = reader.readLine();
System.out.println(line);
} catch (IOException e2) {
// TODO Auto-generated catch block
//e2.printStackTrace();
//JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);
}
}
}
}
}
For testing purposes I had it print out each line of the .txt. Instead of printing out what is in the .txt file it'll print out the first line on and on. Regardless if there is one or 100 lines in the .txt file it'll just constantly read one line. How do I make it read the next and stop when there are no more?
You're creating a new BufferedReader against the file inside the while loop, so the reader is basically reset every time the loop executes. Move the line that creates the reader outside the loop and give it a go.
Move the "new BufferedReader" line outside of the while loop.
if (f.exists()) {
reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
while (line != null) {
try {
//System.out.println("COMES HERE");
line = reader.readLine();
System.out.println(line);
} catch (IOException e2) {
// TODO Auto-generated catch block
//e2.printStackTrace();
//JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With