我从Lynda Java Essential Training 2016第70集中复制了以下代码:
String sourceFile="files/copytest.txt";
String targetFile="files/copied.txt";
try(FileReader fReader=new FileReader(sourceFile);
BufferedReader bReader=new BufferedReader(fReader);
FileWriter writer=new FileWriter(targetFile) ) {
while(true){
String line=bReader.readLine();
if(line==null){
break;
}
else{
writer.write("jarek"+"\n");
}
}
System.out.println("File copied succesfull!");
}catch(Exception e) {
e.printStackTrace();
}
}
当我运行上面的代码时,它会抛出以下错误:
java.io.FileNotFoundException: files\copytest.txt (The system can not find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at de.exercise.copyfile.Copyfile.main(Copyfile.java:108)
我复制了新代码以从txt文件中读取:
File file = new File("files/copytest.txt");
Scanner in = null;
try {
in = new Scanner(file);
String zdanie = in.nextLine();
System.out.println(zdanie);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
但结果是一样的:
java.io.FileNotFoundException: files\copytest.txt (The system can not find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init> (Unknown Source)
at java.util.Scanner.<init> (Unknown Source)
at de.exercise.copyfile.Copyfile.main(Copyfile.java:18)
答案 0 :(得分:0)
首先,避免使用 #container {
width: 50%;
}
}
捕获异常。你应该只抓住预期的那些。
其次,你说过:Exception
。由name定义的java.io.FileNotFoundException: files\copytest.txt (The system can not find the path specified)
实例仅获取项目主文件夹中的文件,即应该在哪里。
如果您使用的是NetBeans,则可能在
中MyDocuments&gt; NetBeanProjects&gt;项目
无论项目是什么,它都应该在第一级内部。像这样:
C:\我的文档\ NetBeanProjects \项目\文件\ copytest.txt
另外,尝试使用不同的反斜杠。你有String /而不是。
我希望我有所帮助。
祝你有个愉快的一天。 :)