我的文件程序工作正常。它使用我提交的内容创建一个 txt 文件。但是,为什么我的控制台会无限显示“真”?
Scanner input = new Scanner(System.in);
System.out.println("What would you like the file to be called?");
String output = input.nextLine();
if (!output.contains(".txt")) {
output += ".txt";
}
PrintWriter out = new PrintWriter(output);
System.out.println("What would you like to print to the file? ");
String fileContents = input.nextLine();
out.print(fileContents);
out.close();
Scanner fromFile = new Scanner(new File(output));
System.out.println("The File " + output + " contains: ");
while (fromFile.hasNextLine()) {
System.out.println(fromFile.hasNextLine());
}
System.out.println("Finished reading and writing from file.. ");
input.close();
fromFile.close();
}