文件拷贝java.io.FileNotFoundException: XXX(拒绝访问)

  • 拒绝访问,如果图片后缀名为 .png 就抛出异常,无法访问,目录也是一样。
public static void main(String[] args) throws  Exception{
        FileUtil fu = new FileUtil("src/io/img/FIleInputStream.png","src/io/copyImg.png");
        fu.copyFile();
    }
"D:\Program Files\Java\jdk1.8.0_211\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58568:D:\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_211\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_211\jre\lib\rt.jar;F:\JavaProject\Java1_3\out\production\Java1_3" io.CopyFileDome
Exception in thread "main" java.io.FileNotFoundException: src\io (拒绝访问。)
	at java.io.FileOutputStream.open0(Native Method)
	at java.io.FileOutputStream.open(FileOutputStream.java:270)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
	at io.FileUtil.copyFile(CopyFileDome.java:43)
	at io.CopyFileDome.main(CopyFileDome.java:13)

Process finished with exit code 1
  • 更改为JPG后缀,就好了。
    public static void main(String[] args) throws  Exception{
        FileUtil fu = new FileUtil("src/io/img/FIleInputStream.png","src/io/copyImg.jpg");
        fu.copyFile();
    }
  • 完整代码【字节流拷贝】
package io;

import java.io.*;

/**
 * @ClassName: CopyFileDome
 * @Author: Xlu103
 * @Date: 2020-06-02 17:49
 **/
public class CopyFileDome {
    public static void main(String[] args) throws  Exception{
        FileUtil fu = new FileUtil("src/io/img/FIleInputStream.png","src/io/copyImg.jpg");
        fu.copyFile();
    }
}

class FileUtil {
    File srcFile;
    File desFile;

    public FileUtil(String srcString, String desString) {
        this.srcFile = new File(srcString);
        this.desFile = new File(desString);
    }

    public FileUtil(File srcFile, File desFile) {
        this.srcFile = srcFile;
        this.desFile = desFile;
    }

    public boolean copyFile() throws Exception {
        if (!this.srcFile.exists()) {
            System.out.println("源文件空,拷贝失败");
            //源文件空,拷贝失败
            return false;
        }
        //如果不存在就创建
        if (!desFile.getParentFile().exists()) {
            desFile.getParentFile().mkdirs();
        }

        InputStream in = new FileInputStream(srcFile);
        OutputStream out = new FileOutputStream(desFile);
        try {
            //用字节流拷贝
            byte[] b = new byte[1024];
            int len;
            long startTime = System.currentTimeMillis();
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
            long endTIme = System.currentTimeMillis();
            System.out.println("文件拷贝完成!耗费时间:"+(endTIme-startTime));
        } catch (Exception e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
        return true;

    }

}

`

  • 完整代码【文件夹拷贝】
package io;

import java.io.*;

/**
 * @ClassName: CopyDirDome
 * @Author: Xlu103
 * @Date: 2020-06-02 19:49
 **/
public class CopyDirDome {
    public static void main(String[] args) throws Exception{
        CopyDir copyDir = null;
        File srcFile = new File("src/api");
        File desFile = new File("src/io/desPath");
        copyDir = new CopyDir(srcFile, desFile);
        boolean result = false;

        long startTime = System.currentTimeMillis();
        //判断是目录还是文件
        if (srcFile.isDirectory()) {
            result=copyDir.copyDir();
        } else if (srcFile.isFile()) {
            result=copyDir.copyFile();
        }
        long endTime = System.currentTimeMillis();
        if (result) {
            System.out.println("拷贝成功,拷贝时间:"+(endTime-startTime));
        }
    }
}

class CopyDir {
    File srcFile;
    File desFile;
    public CopyDir(File srcFile, File desFile) {
        this.srcFile = srcFile;
        this.desFile = desFile;
    }

    public boolean copyDir()throws  Exception {
        //如果目标目录不存在
        if (!desFile.exists()) {
            desFile.mkdirs();
        }
        return copyImpl(this.srcFile);
    }

    //递归操作
    public boolean copyImpl(File file) throws  Exception{
        if (file.isDirectory()){
            File[] result = file.listFiles();
            if (result != null) {
                for (int i = 0; i < result.length;i++) {
                    copyImpl(result[i]);
                }
            }
        }else{
            //这里要执行替换,要把原始目录替换掉
            String newDesPath=file.getPath().replace(this.srcFile.getPath()+File.separator,(""));
            File newFile = new File(this.desFile, newDesPath);
            copyFileImpl(file, newFile);
        }
        return true;
    }

    public boolean copyFileImpl(File srcFile,File desFile) throws Exception{
        if (!srcFile.exists()) {
            System.out.println("源文件空,拷贝失败");
            //源文件空,拷贝失败
            return false;
        }
        if (!desFile.getParentFile().exists()) {
            desFile.getParentFile().mkdirs();
        }

        InputStream in = null;
        OutputStream out =null;

        try {
            in = new FileInputStream(srcFile);
            out = new FileOutputStream(desFile);
            //用字节流拷贝
            byte[] b = new byte[1024];
            int len;
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
        return true;
    }

    public boolean copyFile() throws Exception {
        return copyFileImpl(this.srcFile,this.desFile);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值