這是我的代碼片段:BufferedReader中找不到文件
static String filepath = "test.txt";
public static void main(String[] args) throws IOException {
try{
BufferedReader reader = new BufferedReader(new FileReader(filepath));
String l = reader.readLine();
System.out.println(l);
}catch (FileNotFoundException e){
System.out.println(e);
}
catch(IOException e){
System.out.println(e);
}
java.io.FileNotFoundException: test.txt (No such file or directory)
這是我得到的例外。
我應該使用什麼文件路徑?我把文本文件放在bin
文件夾中。
我在Ubuntu上運行Eclipse,它很重要。
嘗試把文件放在你運行'java'的地方,bin在你的類路徑中,閱讀它你也可以使用Class.getResourceAsStream() – Safrain
你應該必須在FileReader中包含文件路徑。文件路徑不是文件的名稱。 – user1658435