package com.itheima; /* * 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt * * 不会,IO还没自学到*/ import java.io.*; public class Test9 { public static void main(String[] args) { File file=new File("初始路径"); File[] files=file.listFiles(new FilenameFilter(){ //@Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub if(dir.isDirectory()&&name.endsWith(".java")) return true; else return false; } }); BufferedWriter bufw=null; BufferedReader bufr=null; if(file.exists()){ for (int i = 0; i < files.length; i++) { try { bufw=new BufferedWriter(new FileWriter("目的路径"+setName(files[i].getName()))); bufr=new BufferedReader(new FileReader(files[i])); String s=null; while((s=bufr.readLine())!=null) { bufw.write(s); bufw.newLine(); bufw.flush(); } } catch(IOException e) { throw new RuntimeException("读写异常"); } finally { try { if(bufr!=null) bufr.close(); } catch(IOException e) { throw new RuntimeException("读取异常"); } try { if(bufw!=null) bufw.close(); } catch(IOException e) { throw new RuntimeException("写入异常"); } } } } } public static String setName(String str){ return str.substring(0,str.lastIndexOf("."))+".txt"; } } |