题目:编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。之后,将这些文件中的某一个文件剪切到另外一个目录中。
package gre; import java.io.*; public class FileAccept implements FilenameFilter { String i = null; FileAccept(String s){ i = "."+s; } public boolean accept(File dir,String n){ return n.endsWith(i); } }
package gre; import java.io.*; import java.util.*; public class FileD { public static void main(String[] args) { System.out.println("输入目录:"); Scanner r = new Scanner(System.in); String a1 = r.nextLine(); File dir = new File(a1); System.out.println("输入文件类型:"); String s2 = r.nextLine(); FileAccept y = new FileAccept(s2); String fList[] = dir.list(y); for(int i=0;i<fList.length;i++){ System.out.println(fList[i]); } System.out.println("输入要剪切的文件"); String g = r.nextLine(); File dir2 = new File(g); String FilePath = "D:\ZJavaTest1"+"\"+g; BufferedReader in = null; BufferedWriter w =null; try { in = new BufferedReader(new FileReader(g)); w = new BufferedWriter(new FileWriter(FilePath)); String line = null; while((line=in.readLine())!=null) { System.out.println(line); w.write(line); } try { in.close(); w.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果