[英]Fail to copy file (access denied) even though I ran as admin
我正在尝试制作一个将文件从一个驱动器复制到另一个驱动器的应用程序,但由于某种原因,它一直给我一个错误,说我没有写入权限,即使我以管理员身份运行并且运行该应用程序的帐户也是如此是设备上的唯一帐户。 我知道我有权前往目的地,因为
另外:我在 windows 10
复制 function:
private void c2cTransfer(Path source, Path dest) throws Exception {
// Get channel for output file
FileOutputStream fos = new FileOutputStream(dest.toFile());
WritableByteChannel targetChannel = fos.getChannel();
// Get channel for input files
FileInputStream fis = new FileInputStream(source.toString());
FileChannel inputChannel = fis.getChannel();
System.out.print("Closing all IO readers...\t");
// Transfer data from input channel to output channel
inputChannel.transferTo(0, inputChannel.size(), targetChannel);
// close the input channel
inputChannel.close();
fis.close();
// finally close the target channel
targetChannel.close();
fos.close();
}
调用 function 的地方:
if (newPath.toFile().canWrite()) {
c2cTransfer(f, newPath); // Start transfer
System.out.printf("Complete!\n");
} else {
// Keep jumping to here.
System.err.printf("Failed!\nYou do not have permission to write to: %s\n", newLocation);
failedTransfer.add(f);
}
原来需要事先存在的文件,所以我的解决方案是:
if (!newPath.toFile().exists()) {
newPath.toFile().getParentFile().mkdirs();
newPath.toFile().createNewFile();
}
尽管我有一两个文件仍然拒绝访问,即使它存在并且我拥有它的完全权限。 所以,我只是做了一个覆盖 function ,它只是删除旧文件并写入一个新副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.