package cn.gp.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class File2FileDemo {
public static void main(String[] args) throws Exception{
OutputStream outputStream = new FileOutputStream(new File("d:\\2.txt"));
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
BufferedReader bf = null;
BufferedWriter bw = new BufferedWriter(writer);
File dir = new File("D:\\1");
for(File f:dir.listFiles()){
System.out.println(f.getName());
if(f.isDirectory()){
//如果是文件那么进入更深层次的目录
bw.write("-------------------"+f.getName()+"-----------------------------\r\n");
for(File ff: f.listFiles()){
//得到文件名
String filename = ff.getName();
//将文件名单独一行打印,然后将文件逐行读出打印到输出流中
if(filename.endsWith("txt")){
InputStreamReader inputstream = new InputStreamReader(new FileInputStream(ff));
bf = new BufferedReader(inputstream);
String str = "";
bw.write("******************************"+filename+"******************************\r\n");
while((str=bf.readLine())!=null){
System.out.println((str=bf.readLine())+"........");
if(str != null){
bw.write(str+"\r\n");
}
}
}
}
}
}
bw.close();
bf.close();
}
}