Java文件夾的拷貝


  1. 文件夾的拷貝
public static void copyDir(String sourcePath, String newPath) {
		File start = new File(sourcePath);
		File end = new File(newPath);
		String[] filePath = start.list();		//獲取該文件夾下的所有文件以及目錄的名字
		if(!end.exists()) {
			end.mkdir();
		}
		for(String temp:filePath) {
			//查看其數組中每一個是文件還是文件夾
			if(new File(sourcePath+File.separator+temp).isDirectory()) {
				//為文件夾,進行遞歸
				copyDir(sourcePath+File.separator+temp, newPath+File.separator+temp);
			}else {
				//為文件則進行拷貝
				copyFile(sourcePath+File.separator+temp, newPath+File.separator+temp);
			}
		}
	}

2.文件的拷貝

public static void copyFile(String sourcePath, String newPath) {
		File start = new File(sourcePath);
		File end = new File(newPath);
		try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(start));
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(end))) {
			int len = 0;
			byte[] flush = new byte[1024];
			while((len=bis.read(flush)) != -1) {
				bos.write(flush, 0, len);
			}
			bos.flush();
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}

注意:在該函數中,用到的是java7增強的try語句來進行關閉資源。它允許在try關鍵字后緊跟一對圓括號,里面可以聲明、初始化一個或多個資源(不同的資源之間用分號隔開),此處的資源指的是那些必須在程序結束時顯示關閉的資源(數據庫連接、網絡連接等),try語句會在該語句結束時自動關閉這些資源。

3. 函數的調用

public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("From:");
		String sourcePath = scanner.nextLine();
		System.out.print("To:");
		String newPath = scanner.nextLine();
		copyDir(sourcePath, newPath);
	}

4. 源代碼

/**
 * 
 * 復制文件夾d:/java下面所有文件和子文件夾內容到d:/java2。
    提示:涉及單個文件復制、目錄的創建、遞歸的使用
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Practice01{
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("From:");
		String sourcePath = scanner.nextLine();
		System.out.print("To:");
		String newPath = scanner.nextLine();
		copyDir(sourcePath, newPath);
	}
	
	//文件夾的拷貝
	public static void copyDir(String sourcePath, String newPath) {
		File start = new File(sourcePath);
		File end = new File(newPath);
		String[] filePath = start.list();		//獲取該文件夾下的所有文件以及目錄的名字
		if(!end.exists()) {
			end.mkdir();
		}
		for(String temp:filePath) {
			//查看其數組中每一個是文件還是文件夾
			if(new File(sourcePath+File.separator+temp).isDirectory()) {
				//為文件夾,進行遞歸
				copyDir(sourcePath+File.separator+temp, newPath+File.separator+temp);
			}else {
				//為文件則進行拷貝
				copyFile(sourcePath+File.separator+temp, newPath+File.separator+temp);
			}
		}
	}
	
	//文件的拷貝
	public static void copyFile(String sourcePath, String newPath) {
		File start = new File(sourcePath);
		File end = new File(newPath);
		try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(start));
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(end))) {
			int len = 0;
			byte[] flush = new byte[1024];
			while((len=bis.read(flush)) != -1) {
				bos.write(flush, 0, len);
			}
			bos.flush();
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
}

當然和文件以及流有關的更多操作,可以使用Apache Software Foundation提供的相關jar包(commons-io)。

ps:commons-io 下載地址


免責聲明!

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



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