【发布时间】:2020-07-23 02:46:55
【问题描述】:
我正在尝试制作一个将文件从一个驱动器复制到另一个驱动器的应用程序,但由于某种原因,它一直给我一个错误,说我没有写入权限,即使我以管理员身份运行并且帐户是运行此应用程序是设备上的唯一帐户。我知道我有权前往目的地,因为
- 大量文件(共 n 个文件)能够成功复制到目标位置
- 我已授予用户对该目录的完全权限
- 目标是外部存储驱动器。
- 我可以在目标目录中创建一个新文件
另外:我在 Windows 10 上
复制功能:
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();
}
函数被调用的地方:
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);
}
【问题讨论】:
-
确保您可以访问驱动器的两个部分。您要处理的驱动器和要粘贴的驱动器。
-
@AWellPlacedCactus 我有,我有
-
目标文件是否已经存在于newPath?
-
你是 a) 试图写入一个已经存在的文件 b) 写入一个与现有目录同名的文件 c) 试图写入一个具有不寻常文件名的文件(即. 有控制字符) d) 目标驱动器上的空间是否已用完?
-
您可以尝试摆脱检查 newPath.toFile().canWrite() 并编写它以查看抛出了什么异常。如果您无法写入文件,则您的代码假定您没有权限,但这可能不是原因。