编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。之后,将这些文件中的某一个文件剪切到另外一个目录中。
package Test; import java.io.*; public class Test18 { public static void main(String[] args) { // TODO Auto-generated method stub // show("D","txt"); cut("D","test.txt","G"); } public static void show(String dir,String type) { File file = new File(dir+":\"); String[] name = file.list(); for(String name1 : name) { if(name1.endsWith("."+type)) System.out.println(name1); } } public static void cut(String fromDir,String name,String toDir) { FileWriter fw = null; FileReader fr = null; BufferedWriter bufw = null; BufferedReader bufr = null; File file = new File(fromDir+":\"+name); try { fw = new FileWriter(toDir+":\"+name); bufw = new BufferedWriter(fw); fr = new FileReader(file); bufr = new BufferedReader(fr); String str = null; while((str = bufr.readLine())!=null) { bufw.write(str); bufw.newLine(); } System.out.println("剪切成功!!!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if(bufr!=null) bufr.close(); if(bufw!=null) bufw.close(); if(fr!=null) fr.close(); if(fw!=null) fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } file.delete(); } } }