完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径。)

完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径。)

错误原因

读出文件的路径需要有被拷贝的文件名,否则无法解析地址

Alt 错误示例

源代码(用于拷贝)

package com.javase.IO.Stream;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyPicture {
    @Test
    public void copy(){
        File srcFile = new File("D:\\test.jpg");
        File destFile = new File("F:\\");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            byte[] bytes = new byte[1024 * 1024];
            int len = 0;
            while((len = fis.read(bytes)) != -1){
                fos.write(bytes,0,len);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

修改后的代码

package com.javase.IO.Stream;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyPicture {
    @Test
    public void copy(){
        File srcFile = new File("D:\\test.jpg");
        File destFile = new File("F:\\test.jpg");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            byte[] bytes = new byte[1024 * 1024];
            int len = 0;
            while((len = fis.read(bytes)) != -1){
                fos.write(bytes,0,len);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

posted @ 2021-03-12 15:41  卷卷丶  阅读(7015)  评论(1编辑  收藏  举报