Java使用IO流拷貝文件和拷貝目錄

Java使用IO流拷貝文件和拷貝目錄

文件的拷貝

import java.io.*;

public class Copy {
	public static void main(String[] args) {
		//調用方法執行文件複製
		boolean isSuccess = copyFile("要複製的文件(全路徑或相對路徑+文件名)","新的文件名");
		if(isSuccess) {
			System.out.println("文件複製成功!");
		}else {
			System.out.println("文件複製失敗!");
		}
	}
	
	private static boolean copyFile(String oldFile, String newFile) {
		//定義輸入輸出流
		InputStream is = null;
		OutputStream os = null;
		StringBuilder builder = new StringBuilder();
		try {
			//讀取
			is = new FileInputStream(oldFile);
			//輸出
			os = new FileOutputStream(newFile);
			//定義字節緩衝數組
			byte[] flush = new byte[1024];
            //定義讀取長度
            int len;
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            os.flush();
		}  catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //先開啓的流 後關閉
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (new File(newFile).exists()) {
            return true;
        }
        return false;
	}
}

目錄的拷貝

import java.io.*;
import java.time.Duration;
import java.time.Instant;

public class CopyDir {
    public static void main(String[] args) {
        Instant start = Instant.now();
        boolean isOk = dirOrFile("要複製的目錄路徑", "目標路徑");
        if(isOk){
            System.out.println("目錄複製完成!");
        }else {
            System.out.println("目錄複製失敗!");
        }
        Instant end = Instant.now();
        System.out.println("耗時:"+Duration.between(start,end).toMillis()+"毫秒!");
    }

	private static boolean copyDir(String oldDir, String newDir) {
        //定義流
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(oldDir);
            os = new FileOutputStream(newDir);
            //定義緩衝字節數組
            byte[] flush = new byte[1024];
            //定義讀取長度
            int len;
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            //刷新流
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //關閉流
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

	private static boolean dirOrFile(String oldDir, String newDir) {
        //定義寫入寫出文件
        File old = new File(oldDir);
        File copy = new File(newDir);
        //判斷如果是目錄的話 創建目錄
        if (old.isDirectory()) {
            copy.mkdir();
            File[] files = old.listFiles();
            for (int i = 0; i < files.length; i++) {
                dirOrFile(files[i].getAbsolutePath(), newDir + "/" + files[i].getName());
            }
        }
        if (old.isFile()) {
            copyDir(oldDir, newDir);
        }
        if (old.length()==copy.length()){
            return true;
        }
        return false;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章