자바 복사 폴 더

1117 단어
package util;

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

/**
 *        
 * @author Administrator
 *
 */
public class FileUtils {
	/**
	 *         
	 */
	public static void copy(File from, File to){
		if(from.isDirectory()){
			//     
			if(!to.exists())
				to.mkdirs();
			File[] children = from.listFiles();
			for(File f : children){
				String path = to.getAbsolutePath() + "/" + f.getName();
				copy(f, new File(path));
			}
		}else{
			//  
			try {
				InputStream is = new FileInputStream(from);
				OutputStream os = new FileOutputStream(to);
				byte[] bts = new byte[2048];
				int len = is.read(bts);
				while(len > 0){
					os.write(bts, 0, len);
					len = is.read(bts);
				}
				is.close();
				os.close();
				System.out.println("     :" + to.getAbsolutePath());
			} catch (Exception e) {
				throw new RuntimeException("   " + from.getAbsolutePath() + "  " + to.getAbsolutePath() + "  !", e);
			}
		}
	}
}

좋은 웹페이지 즐겨찾기