我得到這個例外,當我嘗試從文件讀取:文件未發現後拋出IOException異常
ERROR:
Exception in thread "main" java.io.FileNotFoundException: newfile.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at Postal.main(Postal.java:19)
import java.util.Scanner;
import java.io.*;
public class Postal {
public static void main(String[] args) throws Exception {
/*** Local Variables ***/
String line;
Scanner filescan;
File file = new File("newfile.txt");
filescan = new Scanner(file);
userInfo book = new userInfo();
/*** Loop through newfile.txt ***/
while (filescan.hasNext()) {
line = filescan.nextLine();
book.addNew(line);
}
book.print(0);
}
}
你得到一個運行時異常,而不是一個編譯器錯誤。在類路徑中找不到文件的原因。 – 2015-01-21 00:42:44
@JunedAsan)1)是(2)否,它在當前目錄中不可用* – EJP 2015-01-30 00:24:46