Java错误:java.io.FileNotFoundException:C:\ Users \ FSSD \ Desktop \ My Test(访问被拒绝) [英] Error in Java : java.io.FileNotFoundException: C:\Users\FSSD\Desktop\My Test (Access is denied)

查看:2883
本文介绍了Java错误:java.io.FileNotFoundException:C:\ Users \ FSSD \ Desktop \ My Test(访问被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a java code for copy file from one folder to another folder. I used the following code (I used Windows 7 operating system),

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

}

}

当我使用上面的代码时,出现以下错误.为什么会出现?我该怎么解决?

While I'm using the above code I got the following Error. Why it will arise? how can I resolved it?

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)

推荐答案

拒绝访问与

Access Denied has to do with User Account Control. Basically, you're trying to read a file which you don't have permission to read (see the file permission under File properties).

通过执行File.canRead()方法,可以查看文件是否可读.

You can see if the file is readable by doing File.canRead() method.

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

}

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

To set it to readable, use the File.setReadable(true) method.

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

或者,您可以使用 java.io.FilePermission 提供文件读取权限.

Alternatively you can use java.io.FilePermission to provide file read permission.

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

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

这篇关于Java错误:java.io.FileNotFoundException:C:\ Users \ FSSD \ Desktop \ My Test(访问被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆