2014-06-13 150 views
-2

我決定測試文件生成Java的可能性所提供:路徑不存在

我試圖讓創建一個系統桌面上的文件的程序。

爲了讓位置,我做了以下內容:

String targetLoc = System.getProperties("user.home") + "/Desktop"; //Returns /Users/targetUser/Desktop 

並且接着創建文件:

File file = new File(targetLoc + "/testfile.txt"); //I'm aware of the slash before the name :) 

try{ 

file.createNewFile(); 

}catch(Exception exception){ 

exception.printStackTrace(); 

} 

即使我沒有看到上面的任何錯誤,我得到的InvalidPathException

爲什麼我會得到那個錯誤?

堆棧跟蹤在請求:

java.io.IOException: No such file or directory 
at java.io.UnixFileSystem.createFileExclusively(Native Method) 
at java.io.File.createNewFile(File.java:883) 
at com.Code0.FileCreater.Main.MainFF.main(MainFF.java:41) 

答:

這是一個簡單的擰起來,我分配了錯誤的值到家庭字符串變量。

+0

你得到的權利,在該位置創建該文件? – thumbmunkeys

+0

你怎麼能夠調用System.getProperties(「user.home」)? – manouti

+0

我只在我的電腦上運行這個功能,如果你的意思是@thumbmunkeys – Code0

回答

0

這是一個簡單的錯誤,我把錯誤的值賦給了home String變量。

非常感謝大家的支持。

問候,

編碼0