import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 题目:
编写一个程序,将指定目录下所有java文件拷贝到另一个目录中
*
*
*
*/
public class Demo04 {
public static void main() throws IOException{
//创建file对象
File file1 = new File("E:\\黑马\\day02");
File file2 = new File("D:\\黑马");
System.out.println("执行了");
copyFile(file1,file2);
}
public static void copyFile(File file1,File file2) throws IOException{
//文件夹
File[] files = file1.listFiles();
//遍历file数组,得到每个file对象
for(File file : files){
//判断file对象是否为文件
if(file.isFile()){
//获取file的绝对路径
String fileName = file.getAbsolutePath();
System.out .println("执行了");
//file的绝对路径中是否包含java
if(fileName.contains("java")){
/*//创建输入流
BufferedReader in = new BufferedReader(new FileReader(file));
//创建输出流
BufferedWriter out = new BufferedWriter(new FileWriter(file2));
//将输入流中的数据写到输出流中
String str = null;
while((str = in.readLine()) != null){
out.write(str);
//刷新流
out.flush();*/
//创建输入流
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
//创建输出流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
//将输入流中的数据写到输出流中
int len = -1;
byte[] str = new byte[1024];
while((len = in.read(str)) != -1){
out.write(str,0,len);
//刷新流
out.flush();
}
System.out .println("执行了");
//关闭流
in.close();
out.close();
}
}else{
copyFile(file,file2);
}
}
}
}
|
|