繁体   English   中英

系统找不到指定的路径

[英]System can not find the path specified

我正在尝试使用Java中的createNewFile()方法创建一个新文件:

File savegame = new File(System.getenv("APPDATA") + File.separator + "Game" + File.separator + "test" + ".ser");

try
{
    savegame.createNewFile();
} 
catch(IOException exc)
{
    exc.printStackTrace();
}

但是我得到一个IOException,它表示系统找不到指定的路径,并且无法理解为什么?

确保应在其中创建文件的目录存在。 要在创建文件之前创建目录,可以执行以下操作:

File savegame = new File(System.getenv("APPDATA") + File.separator + "Game" + File.separator + "test" + ".ser");

try
{
    savegame.getParentFile().mkdirs();  // create parent directory
    savegame.createNewFile();
} 
catch(IOException exc)
{
    exc.printStackTrace();
}

File#mkdirs()的文档中:

创建以此抽象路径名命名的目录,包括任何必需但不存在的父目录。 请注意,如果此操作失败,则可能已成功创建了一些必要的父目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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