如何在读取.txt文件java时在行中包含空格和其他字符



我有一个0.txt文件,其中包含:4行,如下所示:


help 
hello world
hello world help

我有一个代码,如果我读txt文件,它会忽略空格:

public static void Read_With_Spaces(String FileName) {
File file = new File(FileName);
try {
Scanner fileReader = new Scanner(file);
while (fileReader.hasNext()) {
String word = fileReader.next();
System.out.println(word + " " + "health");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

我需要有空位的阅读。请帮帮我!

您可以像使用fileReader.hasNextLine()一样使用hasNextLine(),然后像在fileReader.nextLine()中那样使用nextLine()读取行

最新更新