複製文件以及異常處理

代碼實現特定 文件的複製粘貼。
在這裏插入圖片描述
如將D盤的大作業複製到F盤。
出現過拒絕訪問的錯誤:你要讀取文件,但路徑是目錄。

public class Demo1 {
    public static void main(String[] args) throws IOException {
        File srcPath = new File("D:\\大作業");
        File dstPath = new File("E:\\");
        copyDir(srcPath,dstPath);
    }

    private static void copyDir(File srcPath,File dstpath) throws IOException{
        if(srcPath.isDirectory()){
            //要複製的是一個文件夾
            File newFolder = new File(dstpath, srcPath.getName());//更新目錄
            if(!newFolder.exists()) newFolder.mkdir();//創建目錄
            File[] files = srcPath.listFiles();//得到目錄下的文件
            for (File f:files//遍歷文件
                 ) {
                copyDir(f,newFolder);
            }
        }else copy(srcPath,dstpath);

    }

    private static void copy(File srcfile, File dstPath) throws IOException {
     //字節緩衝流讀寫
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));//字節緩衝輸入流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));//字節緩衝輸出流
        int len;
        byte[] b=new byte[1024];
        while((len=bufferedInputStream.read(b))!=-1){
            bufferedOutputStream.write(b,0,len);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

  1. try catch finally
    private static void copy(File srcfile, File dstPath)  {
        BufferedInputStream bufferedInputStream = null;//字節緩衝輸入流
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        BufferedOutputStream bufferedOutputStream = null;//字節緩衝輸出流
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));
            bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));
            int len;
            byte[] b=new byte[1024];
            while((len=bufferedInputStream.read(b))!=-1){
                bufferedOutputStream.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedOutputStream!=null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  1. JDK 7以後可以
try(定義流對象){
	...
}catch(...){
	...
}

自動釋放資源,不用close
    private static void copy(File srcfile, File dstPath)  {
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));){
            int len;
            byte[] b=new byte[1024];
            while((len=bufferedInputStream.read(b))!=-1){
                bufferedOutputStream.write(b,0,len);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
  1. JDK 9之後可以
定義流對象
定義流對象
try(流對象){
	
}catch(...){
	....
}
自動釋放資源
private static void copy(File srcfile, File dstPath) throws IOException {
        File dstFile = new File(dstPath,srcfile.getName());//目的文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));//字節緩衝輸入流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));//字節緩衝輸出流
        try(bufferedInputStream;bufferedOutputStream){
            int len;
            byte[] b=new byte[1024];
            while((len=bufferedInputStream.read(b))!=-1){
                bufferedOutputStream.write(b,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章