【问题标题】:Error in Java : java.io.FileNotFoundException: C:\Users\FSSD\Desktop\My Test (Access is denied)Java 中的错误:java.io.FileNotFoundException: C:\Users\FSSD\Desktop\My Test(访问被拒绝)
【发布时间】:2011-10-28 22:49:51
【问题描述】:

我有一个用于将文件从一个文件夹复制到另一个文件夹的 java 代码。我使用了以下代码(我使用的是Windows 7操作系统),

CopyingFolder.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;


public class CopyingFolder {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File infile=new File("C:\\Users\\FSSD\\Desktop\\My Test");
        File opfile=new File("C:\\Users\\FSSD\\Desktop\\OutPut");
        try {
            copyFile(infile,opfile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void copyFile(File sourceFile, File destFile)
            throws IOException {
    if (!sourceFile.exists()) {
            return;
    }
    if (!destFile.exists()) {
            destFile.createNewFile();
    }
    FileChannel source = null;
    FileChannel destination = null;
    source = new FileInputStream(sourceFile).getChannel();
    destination = new FileOutputStream(destFile).getChannel();
    if (destination != null && source != null) {
            destination.transferFrom(source, 0, source.size());
    }
    if (source != null) {
            source.close();
    }
    if (destination != null) {
            destination.close();
    }

}

}

当我使用上面的代码时,我得到了以下错误。为什么会出现?我该如何解决?

java.io.FileNotFoundException: C:\Users\FSSD\Desktop\My Test (Access is denied)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at CopyingFolder.copyFile(CopyingFolder.java:34)
    at CopyingFolder.main(CopyingFolder.java:18)

【问题讨论】:

  • 你是在 FSSD 用户名下运行这个吗?
  • 你确定My Testoutput 是文件而不是目录
  • My Test 是文件还是目录?此外,您不需要显式调用 createNewFile(),因为创建 FileOutputStream 会在必要时创建它。
  • @vickirk:是的,这些文件夹名称分别是源和目标
  • 那么你的代码就是错误的:你应该检查它是否是一个目录,如果是则递归到它。打开FileInputStream 仅对文件有意义(即非目录)。

标签: java file windows-7 hidden


【解决方案1】:

拒绝访问与User Account Control 有关。基本上,您正在尝试读取您无权读取的文件(请参阅文件属性下的文件权限)。

你可以通过File.canRead()方法查看文件是否可读。

if (infile.canRead()) {
    //We can read from it.

}

要将其设置为可读,请使用File.setReadable(true) 方法。

if (!infile.canRead()) {
   infile.setReadable(true);
}

您也可以使用java.io.FilePermission 提供文件读取权限。

FilePermission permission = new FilePermission("C:\\Users\\FSSD\\Desktop\\My Test", "read");

或者

FilePermission permission = new FilePermission("C:\\Users\\FSSD\\Desktop\\My Test", FilePermission.READ);

【讨论】:

  • 我使用了你的代码,我无法得到正确的输出。我想,当我添加这个时我犯了错误..如果你不介意按照你的代码更改我的代码,
  • @BenDennison,我不知道你的目录结构和你在做什么。给我更多关于代码在做什么的信息。
  • 我的要求是,我想将文件从一个文件夹移动到另一个文件夹。即“C:/MyTest”,在 MyTest 文件夹中,我有一个文件名“ExampleFile.file”。在这里,我必须将“ExampleFile.java”移动到“C:/OutPut”文件夹。如果可以使用我的代码执行此操作,则可以使用其他任何替代方式执行此操作。提前致谢
  • 然后尝试将infile 更改为包含完整 文件名(带路径),如下所示:File infile=new File("C:\\Users\\FSSD\\Desktop\\My Test\\Example.java"); 并查看它是否有效。另外,我的可能会工作,因为我对 Windows 中的所有数据都有管理员访问权限。
【解决方案2】:

我会将我的文件放在不在 user/... 下的目录中...

尝试将文件放在 c:/mytest/ 中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多