java - 复制文件导致输出中出现 java.io.FileNotFoundException(拒绝访问)

标签 java file filenotfoundexception access-denied

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

最佳答案

这里的问题是因为使用

file1.mkdirs();

file1.createNewFile();

在一起。

由于 file1 对象在通过调用“file1.mkdirs()”将其创建为目录后已被赋予“目录”属性,但是您再次使用相同的对象来创建“file',这意味着将 file1 对象的属性从目录更改为文件,这是不允许的。这就是它为您提供 FileNotFound 的原因。

关于java - 复制文件导致输出中出现 java.io.FileNotFoundException(拒绝访问),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776481/

相关文章:

java - 无法在 try catch 中删除文件

Java同时在多个线程之间访问一个文件

java - scala - bash : hw. scala:权限被拒绝

drawable-hdpi 文件夹中 custom.xml 的 Android FileNotFound 异常

java - Linux java 没有这样的文件或目录,java.io.FileNotFoundException :

java - 在客户端/服务器 java 套接字之间同步时间

java - Kafka+使用prometheus与kafka cli命令有冲突

java - JavaBean 属性命名约定在哪里定义?

python-3.x - HTTP 错误 403 : Forbidden while downloading file using urllib

Java 字符串 : Replace a string containing $ sign