package testProject.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* <p class="detail">
* 功能:写一个文件拷贝函数: fileCopy(String a ,String b)
* a--表示原文件名 b--表示目标文件名扩展:
* 如果a是文件,则copy a到b 文件;
* 如果a是目录,则copy a下的所有文件和文件夹(包括子文件夹)到b目录下。
* </p>
* @ClassName: IoDeme
* @version V1.0
* @date 2016年8月8日
* @author Administrator
* Copyright 2016 tsou.com, Inc. All rights reserved
*/
public class IoDeme {
public static void main(String[] args) {
fileCopy("E:/temp/yuanFile/tt.txt","E:/temp/newFile/tt2.txt");
}
public static void fileCopy(String a, String b){
File file = new File(a);
if ( ! file.exists()){
System.out.println(a + " Not Exists. " );
return ;
}
File fileb = new File(b);
if (file.isFile()){
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(fileb);
byte [] bb = new byte [ ( int )file.length()];
fis.read(bb);
fos.write(bb);
} catch (IOException e){
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (file.isDirectory()){
if ( ! fileb.exists()){
fileb.mkdir();
}
String[] fileList;
fileList = file.list();
for ( int i = 0 ; i < fileList.length; i ++ ){
fileCopy(a + " \\ " + fileList[i],b + " \\ " + fileList[i]);
}
}
}
}