- import java.io.*;
- public class Test9
- {
- public static void main(String[] args)
- {
- File ori = new File("d:\\java\\day12");
- File des = new File("d:\\test");
- copyTo(ori,des);
- }
- public static void copyTo(File ori,File des)
- {
- if(!des.exists() && !des.isDirectory())
- {
- des.mkdir();
- }
- File[] files = ori.listFiles();
- for(File f: files)
- {
- if(f.isDirectory())
- {
- copyTo(f,des);
- }
- else if(f.getName().endsWith(".java"))
- {
- copy(f,des);
- }
- }
- }
- public static void copy(File ori,File des)
- {
- BufferedReader bufr = null;
- PrintWriter out = null;
- try
- {
- bufr= new BufferedReader(new InputStreamReader(new FileInputStream(ori)));
- out = new PrintWriter(des+"\\"+ori.getName().replace(".java",".txt"));
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- out.println(line);
- }
- }
- catch (IOException e)
- {
- System.out.println("文件复制失败!");
- }
- finally
- {
- try
- {
- if(bufr!=null)
- bufr.close();
- }
- catch (IOException e)
- {
- System.out.println("文件写入流关闭失败。");
- }
- try
- {
- if(out!=null)
- out.close();
- }
- catch (Exception e)
- {
- System.out.println("文件打印流关闭失败。");
- }
- }
- }
- }
复制代码 |
|