【问题标题】:Copying a file results in java.io.FileNotFoundException (Access is Denied) from output复制文件会导致输出中的 java.io.FileNotFoundException (Access is Denied)
【发布时间】:2013-07-21 20:17:22
【问题描述】:
File file1 = new File(file.getAbsoluteFile() + "/example/directory/example.png");
file1.mkdirs();
file1.setWritable(true);
file1.createNewFile();
try {
    FileInputStream is = new FileInputStream(exampleInputDirectory);
    FileOutputStream os = new FileOutputStream(file1);
    FileChannel srcChannel = is.getChannel();
    FileChannel dstChannel = os.getChannel();
    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    is.close();
    os.close();
} catch (IOException e) {
    e.printStackTrace();
}

这是我将图像文件复制到新目录树的设置。但是,当执行此代码时,我得到以下信息:

java.io.FileNotFoundException: *points to output directory* (Access is denied)

我是否错误地创建了file1

【问题讨论】:

  • 你在第一行写了file.getAbsoluteFile()。您是否还有另一个名为 file 的变量,或者您的意思是 file1
  • 文件正在被使用吗?您是否有权访问该路径?
  • 已更新答案,看看吧。

标签: java file filenotfoundexception access-denied


【解决方案1】:

这里的问题是因为使用

file1.mkdirs();

file1.createNewFile();

一起。

由于file1 对象在通过调用“file1.mkdirs()”将其创建为目录后已被赋予“目录”属性,但随后您再次使用同一对象创建“文件”,这意味着将 file1 对象的属性从目录更改为文件,这是不允许的。这就是为什么它给你FileNotFound

【讨论】:

  • 我摆脱了 mkdirs 调用。现在它在 file1 上给我java.io.IOException: The system cannot find the path specified。我应该制作目录然后制作文件吗?
  • @JeffDemanche 是的,因为 createNewFile() 只会创建一个新文件。但是如果路径不存在,它不会创建目录。这就是为什么它现在给出这个错误。因此,请确保您尝试将文件放入的文件夹存在。
【解决方案2】:

您创建的 file1 似乎达到了标准,也许您的输入目录不存在?确保所有大写字母等都是正确的,并且您的目录参考中没有拼写错误。还要确保用户是否有权将文件复制到目录,如果没有,您可以在 linux 中以 root 身份运行,在 windows 中以管理员身份运行。

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 2017-08-14
    • 2014-04-04
    • 1970-01-01
    • 2012-05-06
    • 2015-05-14
    • 2019-05-04
    • 1970-01-01
    • 2021-01-10
    相关资源
    最近更新 更多