完美解決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();
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM