简体   繁体   中英

Exception “System can't find the file specified” file.createNewFile()

When I try to create a file, it gives me an IOException.

java.io.IOException: The system cannot find the path specified at java.io.WinNTFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(Unknown Source)

I have code that makes settings directory and checks that it is there. Every time I run it, it displays the "New User" message. Here is the code that makes the directory:

f = new File(System.getProperty("user.home") + "AppData\\Local\\DataDude\\pass\\");

and here is the check:

if (f.isDirectory()) {
    firstTime = false;
} else {
    firstTime = true;
    f.mkdirs();
}

Here is where it creates the file:

File f = new File(this.getPassLoc() + user.getText() + ".ser");
if (!f.exists()) {
    f.createNewFile();
} 

( getPassLoc() returns f )

On my system I get the following values for System.getProperty("user.home") and your file f :

C:\Users\Luke
C:\Users\LukeAppData\Local\DataDude\pass

You missed a backslash before AppData , or assumed that System.getProperty("user.home") would end with one, so the logged-in username and AppData have been concatenated into one directory name. I don't believe this is what you want.

Instead of using user.home try going from the main drive root directory and scaling upwards from there. There error is saying it can not find the directory you want it to save the file too.. If you want the error to go away you either need to create that directory structure or you need to let the java program create the directory structure.

f = new File(System.getProperty("user.home") + "AppData\\Local\\DataDude\\pass\\").mkdirs();

once you do this go through and make sure the directories are where you want them.

可能是因为您没有足够的权限写入该文件夹。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM