2012-01-30 80 views
1

我创建简单的对象序列化,并创建BufferedOutputStream正在引发异常AccessDeniedException。下面是代码:为什么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(); 
} 

但目录是空的,如果没有不存在的话,它的创建...

+0

什么是'filePath'?我没有看到声明和价值?请注意,你应该写入一个文件,而不是目录 - 以防万一'filePath'实际上是'“C:\\ temp”'。还要注意名称'xmlFile'表明你有一个文件,但它指向一个目录。 – Thomas 2012-01-30 14:56:32

+0

你是对的。更新 – Eugene 2012-01-30 14:59:21

+0

什么是'路径'? 'filePath'的实际值是多少? – Thomas 2012-01-30 15:01:30

回答

1

在这里我要重复我的意见:你似乎试图写入目录不是文件。尝试将filePath更改为文件。

相关问题