根目錄名爲CopyFile,有目錄文件和src。文件具有文本文件loremipsum.txt。 SRC具有COM /示例/ JAVA/Main.javajava無法使FileReader正常工作
這是Main.java
package com.example.java;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) {
String sourceFile = "files/loremipsum.txt";
String targetFile = "files/target.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(line + "\n");
}
}
System.out.println("File Copied!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面的代碼是錯誤:
java.io.FileNotFoundException: files/loremipsum.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at com.example.java.Main.main(Main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
過程,退出代碼完成0
這意味着文件'文件/ loremipsum.txt'不exsist – 3kings
但其在目錄中? .... java在哪裏看?我甚至用Main.java的目錄中的文本文件創建了一個名爲files的文件夾,並且該文件不起作用 –
最近java有更改嗎?將代碼置於括號內的「try」語句現在是否合法?這個代碼如何編譯? –