I, entitled
Write an application, enter a directory and a file type, display the directory meets all files of that type. Thereafter, the cut one of these files to another directory.
Second, the code
package mulu; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; import java.util.Scanner; public class mulu { public static void main(String[] args) { System.out.println("输入一个目录"); Scanner reader = new Scanner(System.in); String s =reader.next (); File the dir = new new File (S); . the System OUT .println ( " Enter file type " ); Scanner Reader2 = new new Scanner (the System. in ); String K = reader2.next (); FileAccept CON = new new FileAccept (K); String the fileList [] = dir.list (CON); . the System OUT (.println " there directory " + fileList.length + " files " ); for(int i =0;i<fileList.length;i++) { System.out.println(fileList[i]); } System.out.println("输入要剪切的文件"); Scanner reader3 = new Scanner(System.in); String g = reader3.next(); String f = s+"\\"+g; File dir2 = new File(f); String FilePath = "E:\\txt"+"\\"+g; try(BufferedReader in = new BufferedReader(new FileReader(f)); BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath)); ) { String line = null; while((line=in.readLine())!=null) { System.out.println(line); writer.write(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } dir2.delete(); //删除文件 } }class FileAccept implements FilenameFilter{ String type; FileAccept(String type){ this.type = type; } public boolean accept(File dir, String name) { return name.endsWith(type); } }
Third, the operating results