[英]FileNotFoundException errors
對不起,標題不好,想不到更好的標題。
我目前在FileNotfoundException這個矛盾的問題中,通過命令file.getCanonicalPath()
和使用FileInputStream
方法來定位我的文件。 我得到FileNotFoundException 。
以下是我使用的代碼:
File file = new File("members.s");
System.out.println(file.getCanonicalPath());
FileInputStream fileIn = new FileInputStream("C:\\Users\\users\\Documents\\NetBeansProjects\\CWA2\\members.s");
ObjectInputStream in = new ObjectInputStream(fileIn);
byte[] b=new byte[fileIn.available()];
for(int i=0;i<b.length;i++){
m.add(mem = (Member)in.readObject());
}
這是我得到的輸出和異常錯誤。
C:\Users\users\Documents\NetBeansProjects\CWA2\members.s
java.io.FileNotFoundException: C:\Users\users\Documents\NetBeansProjects\CWA2\members.s (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at Demos.DeserializeDemo.main(DeserializeDemo.java:21)
所以我有點困惑。 file.getCanonicalPath()
方法如何找到我要使用的文件,但是FileInputStream
返回錯誤。 誰可以幫我這個事?
file.getCanonicalPath()
只會返回"members.s"
作為其路徑,而不是完整路徑。 getCaninicalPath()
刪除多余的.
或..
從路徑名。
因為FileInputStream
將File
作為其參數(也是字符串btw),而File
將String
作為參數。
File file = new File("members.s");
System.out.println(file.getCanonicalPath());
FileInputStream fileIn = new FileInputStream("C:\\Users\\users\\Documents\\NetBeansProjects\\CWA2\\members.s");
這應該是
File file = new File("C:\\Users\\users\\Documents\\NetBeansProjects\\CWA2\\members.s");
FileInputStream fileIn = new FileInputStream(file);
當具有指定路徑名的文件不存在或該文件確實存在但由於某種原因而無法訪問時(例如,當試圖打開只讀文件進行寫入時),將引發此異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.