import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*请编写程序,复制单层文件夹,并测试*/
public class CopyDirectory {
public static void main(String[] args) throws IOException {
//1.封装数据源,封装目的地
File srcPath = new File("D:\\java\\wokepace\\1day23\\b");
File destPath = new File("D:\\java\\wokepace\\1day23\\copy-b");
//创建目的地文件夹
destPath.mkdirs();
//2:获取到数据源中所有的File对象
File[] files = srcPath.listFiles();
//3:遍历, 得到每一个File对象
for (File file : files) {
//4:复制文件
String name = file.getName();
//组成目的地File对象
File dest = new File(destPath, name);
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
//读取数据
byte[] buffer = new byte[1024];
int len = 0;
while( (len=fis.read(buffer)) != -1){
//写数据到目的地
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
}
|
|