繁体   English   中英

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

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

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

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

另外:我在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM