只拷贝文件不拷贝文件夹的Java程序

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


public class DeepCopy {
	public static void main(String[] args){
		deepCopy("F:/Software/所有字体", "F:/Software/All_Font");
	}
	
	// 将folder_source所有文件(不包括文件夹)全部拷贝到dest_folder,
	private static void deepCopy(String folder_source, String dest_folder){
		File file1 = new File(folder_source);
		if (file1.isFile()){
			copy(file1.getAbsolutePath(), dest_folder+"/"+file1.getName());
		}else if (file1.isDirectory()){
			File[] files2 = file1.listFiles();
			for (int i = 0; i < files2.length; i++){
				deepCopy(files2[i].getAbsolutePath(), dest_folder);
			}
		}
	}
	
	public static boolean copy(String fileFrom, String fileTo) {
		try {
			InputStream in = new FileInputStream(fileFrom);
			OutputStream out = new FileOutputStream(fileTo);
			byte[] bt = new byte[1024];
			int count;
			while ((count = in.read(bt)) > 0) {
				out.write(bt, 0, count);
			}
			in.close();
			out.close();
			System.out.println("Copy Finished!");
			return true;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
	}
	
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。