gpt4 book ai didi

java - 为什么使用刚刚创建的文件夹时会引发 AccessDeniedException?

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:30 26 4
gpt4 key购买 nike

我正在创建简单的对象序列化,并且BufferedOutputStream的创建引发了异常AccessDeniedException。这是代码:

Path filePath = Paths.get("c:\\temp\\");
File xmlFile = new File("c:\\temp\\");
boolean success = xmlFile.mkdirs();
if (!success && ! xmlFile.exists() ) {
// Directory creation failed
System.out.println("Failed to create a file: " + filePath);
}

try (
ObjectOutputStream objectOut = new ObjectOutputStream(
new BufferedOutputStream(Files.newOutputStream(filePath, StandardOpenOption.WRITE)))){
// Write three objects to the fi le
objectOut.writeObject(solarSystem); // Write object

System.out.println("Serialized: " + solarSystem);
} catch(IOException e) {
e.printStackTrace();
}

但是目录是空的,如果它不存在,则会创建它......

最佳答案

我将在这里重复我的评论:您似乎尝试写入目录而不是文件。尝试将 filePath 更改为文件。

关于java - 为什么使用刚刚创建的文件夹时会引发 AccessDeniedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9065801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com