无法复制文件(访问被拒绝),即使我以管理员身份运行也是如此。

huangapple 未分类评论43阅读模式
英文:

Fail to copy file (access denied) even though I ran as admin

问题

我正在尝试制作一个应用程序,用于将文件从一个驱动器复制到另一个驱动器,但由于某种原因,它不断给我一个错误,说我没有写入权限,即使我以管理员身份运行,运行此应用程序的帐户是设备上唯一的帐户。我知道我对目标有写入权限,因为:

  1. 大量文件(共 n 个文件)能够成功复制到目标位置。
  2. 我已经为用户赋予了目录的完全权限。
  3. 目标是外部存储驱动器。
  4. 我可以在该目标目录中创建新文件。

此外:我使用的是 Windows 10。

复制功能:

private void c2cTransfer(Path source, Path dest) throws Exception {
    // 获取输出文件的通道
    FileOutputStream fos = new FileOutputStream(dest.toFile());
    WritableByteChannel targetChannel = fos.getChannel();

    // 获取输入文件的通道
    FileInputStream fis = new FileInputStream(source.toString());
    FileChannel inputChannel = fis.getChannel();

    System.out.print("关闭所有 IO 读取器...\t");
    // 将数据从输入通道传输到输出通道
    inputChannel.transferTo(0, inputChannel.size(), targetChannel);

    // 关闭输入通道
    inputChannel.close();
    fis.close();

    // 最后关闭目标通道
    targetChannel.close();
    fos.close();
}

调用该函数的位置:

if (newPath.toFile().canWrite()) {
    c2cTransfer(f, newPath); // 开始传输
    System.out.printf("完成!\n");
} else {
    // 不断跳转到此处。
    System.err.printf("失败!\n您没有写入权限:%s\n", newLocation);
    failedTransfer.add(f);
}
英文:

I'm trying to make an app that copies files from one drive to another, but for some reason, it keeps giving me an error saying I do not have permission to write even though I ran as admin and the account that is running this app is the only account on the device. I know I have permission to the destination because

  1. A large number of files (out of n files) are able to copy to the destination successfully
  2. I have given the user full permission to the directory
  3. The destination is an external storage drive.
  4. I can make a new file in that destination's directory

Additionally: I'm on windows 10

Copy 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();
}

Where the function is being called:

		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);
		}

答案1

得分: 0

结果发现这些文件需要事先存在,所以我的解决方案是:

if (!newPath.toFile().exists()) {
    newPath.toFile().getParentFile().mkdirs();
    newPath.toFile().createNewFile();
}

虽然我有一两个文件仍然因为访问权限被拒绝而导致出现问题,尽管它们是存在的并且我对它们有完全权限。因此,我只是创建了一个覆盖函数,用它来删除旧文件并写入新副本。

英文:

Turns out the files needed to exist before hand, so my solution was this:

if (!newPath.toFile().exists()) {
	newPath.toFile().getParentFile().mkdirs();
	newPath.toFile().createNewFile();
}

Though I have one or two files that still giving me access denied even though it exist and I have full permission over it. So, I just made a override function that just deletes the old files and writes a new copy.

huangapple
  • 本文由 发表于 2020年7月23日 10:46:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/63046088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定