【发布时间】:2018-10-11 04:32:20
【问题描述】:
我的任务是将一个文件的文本复制到另一个文件。使用以下代码,我可以将文件的行打印到控制台,但无法打印到输出文件。
try {
System.out.print("Enter the file name with extension : ");
Scanner input = new Scanner(System.in);
File inputFile = new File(input.nextLine());
PrintWriter out = new PrintWriter("/Users/jonathanzier/Dropbox/IdeaProjects/CSE_205_Homework1/src/com/company/output.txt");
input = new Scanner(inputFile);
while (input.hasNextLine()) {
out.println(input.nextLine());
//System.out.println(input.nextLine()); - This will print
//correctly to the console
}
out.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
【问题讨论】:
-
显示文件未找到错误
-
尝试添加 out.flush(); out.println(input.nextLine());
-
@iamsankalp89 这是你的系统。如果文件没有找到,那么他应该会看到它。
-
@pavithraCS - 是的,这是为什么呢?谢谢
-
现在我在循环中添加了 out.flush() 它可以正常工作。谢谢!
标签: java