Topic: 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.
Code:
package My; import java.util.*; import java.io.*; class FileAccept implements FilenameFilter{ String type; FileAccept(String type){ this.type = type; } public boolean accept(File dir, String name) { return name.endsWith(type); } } public class Test12_05 { public static void main (String [] args) { // the TODO Auto-Generated Stub Method System.out.println ( "Enter a directory" ); Scanner reader = new Scanner(System.in); String s = reader.next(); File dir = new File(s); System.out.println ( "Enter file type" ); Scanner reader2 = new Scanner(System.in); String k = reader2.next(); FileAccept con = new FileAccept(k); String fileList[] = dir.list(con); System.out.println ( "directory has" + fileList.length + "files" ); for ( int I = 0; I <fileList.length; I ++ ) { System.out.println(fileList[i]); } System.out.println ( "Enter file to cut" ); Scanner reader3 = new Scanner(System.in); G String = reader3.next (); // store the file name String f = S + "\\" + G; // directory previously entered + \ + filename File dir2 = new new File (f); FilePath String = "D: \\ \\ Java.Test New Folder" + "\\" + G; // cut position to the try (in the BufferedReader = new new the BufferedReader ( new new the FileReader (F)); // automatically Close the resource written BufferedWriter, Writer = new new BufferedWriter, ( new 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 (); // delete files } }
operation result:
Cut ago:
Cut the original directory:
Cut back to the directory: