简体   繁体   English

缓冲读取器无法读取我的整个文件Java

[英]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 . 您只能使用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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM