【发布时间】:2017-11-10 02:58:44
【问题描述】:
我想在项目目录中创建一个文件,所以我尝试了这段代码,并在项目目录中创建了一个临时目录
try {
File file = new File("temp/"+soCourrier.getHeaders().get("filename").get(0));
FileOutputStream fos = new FileOutputStream(file);
fos.write(sCourrier.getBody());
model.addAttribute("path", file.getAbsoluteFile());
} catch (IOException e) {
e.printStackTrace();
}
但这给了我一个错误
java.io.IOException: Le chemin d’accès spécifié est introuvable
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at controllers.UserController.showCourrierDetail(UserController.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
谁能帮帮我?
【问题讨论】:
-
给出绝对路径并尝试
-
Java 临时文件不在项目目录中
-
@Jeremy Grand 很抱歉该行应该被评论
-
@gatisahu 你能解释一下如何
-
@WassimMakni java 应用程序应该是可移植的,创建具有相对路径的文件几乎总是注定会失败。因此,请确定要写入文件的文件夹,并从文件系统根目录获取其路径。并在创建文件之前创建父目录。