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/