码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-09-27 01:05:48      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:copy   deepcopy   

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;
		}
	}
	
}

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

标签:copy   deepcopy   

原文地址:http://blog.csdn.net/aimatfuture/article/details/39587697

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!