我是Java初學者,我試圖從現有的文本文件中讀取一個程序。我已經盡力了,但它仍然在說「File Not Found!」。我已將我的「Test.txt」複製到我的包的兩個文件夾 - src和bin。 請幫助我解決這個問題。我會非常感謝。這裏的代碼 -閱讀Java中現有的文本文件
package readingandwritingfiles;
import java.io.*;
public class ShowFile {
public static void main(String[] args) throws Exception{
int i;
FileInputStream file_IN;
try {
file_IN = new FileInputStream(args[0]);
}
catch(FileNotFoundException e) {
System.out.println("File Not Found!");
return;
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: ShowFile File");
return;
}
do {
i = file_IN.read();
if(i != -1)
System.out.print((char)i);
} while(i != -1);
file_IN.close();
System.exit(0);
}
}
它看起來像程序試圖在'args [0]'中找到文件 - 當你運行程序時,你有文件路徑/名稱作爲命令行參數嗎? –
它似乎是通過命令行參數'(args [0])'得到文件的名稱'..你在用什麼IDE? – XDProgrammer
您必須將文件名作爲第一個參數。如果您使用IDE,請檢查傳遞參數的方式。 – Elvermg