简体   繁体   中英

Buffered Reader is not reading my whole file java

BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\NormenYu\\Desktop\\Programming\\Java\\eclipse\\Book\\"+thebook+".txt"));
            String line = reader.readLine();System.out.println(line);

My File:
(tab)You are on a hiking trip with your friend(also lives with you in a rented apartment). You suddenly find yourself walking into a jungle. As you walk, you suddenly find yourself very lonely. “Help!”, you heard. (enter)(tab)“What was that,” you ask your friend. There is no reply. Wait... where is your friend? You start to find your way back, and suddenly you find your friend stuck in quicksand.

Do you: Walk towards your friend and try to save him or Stay away because you might also get stuck in quicksand

The program prints: You are on a hiking trip with your friend(also lives with you in a rented apartment). You suddenly find yourself walking into a jungle. As you walk, you suddenly find yourself very lonely. “Help!”, you heard.

HELP!! By the way, the things in parentheses are not written in the notepad.

You are only reading in one line with the method readLine . You need to loop over the file until you reach the end. Something like this:

BufferedReader in = new BufferedReader(new FileReader(file));

while (in.ready()) {
  String s = in.readLine();
  System.out.println(s);
}
in.close();

Using a loop you can read each line in the file.

BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\NormenYu\\Desktop\\Programming\\Java\\eclipse\\Book\\"+thebook+".txt"));
String line;

while((line = reader.readLine()) != null) {
  System.out.println(line);
}
reader.close()
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\NormenYu\\Desktop\\Programming\\Java\\eclipse\\Book\\"+thebook+".txt"));

String full = "";

String line;
while ((line = reader .readLine()) != null) {
    full += line;
}

// full now contains the whole content of your file.

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