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.
ps: do this before the first class section.
text.java
import java.io.*; import java.util.Scanner; public class text { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.print ( "Please enter a file path:" ); String s1 = sc.nextLine(); System.out.print ( "Please enter the file format suffix:" ); String s2 = sc.nextLine(); Path File = new new File (S1); // file path of the FilenameFilter endName = new new getFilenames (S2); // file suffix String [] of the filenames = path.list (endName); System.out.println ( "There are" + filenames.length + "a" + s2 + "type of file!" ); For (String name: filenames) { // loop output file name matching System.out.println (name ); } } } class getFilenames implements FilenameFilter{ String style ; getFilenames(String style){ this.style=style; } public boolean accept(File file, String name) { return name.endsWith(style); } }
result:
Title II: read and write operations to complete
Code
/ ** Cut files to another directory * Method 2 Class 3 */ import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.Writer; import java.util.Scanner; class of the type the implements FilenameFilter { // create interfaces FilenameFilter subclass String str = null ; type (String STR) { // define a constructor method the this .str = + "." STR; } public Boolean accept (File the dir, String name) { // override method accept return name.endsWith (STR); // matching field behind } } public class Test { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print ( "Please enter the directory: \ the n-" ); Way String = reader.nextLine (); // temporary directory File File = new new File (Way); // get the input directory String [] = All file.list (); // call the no-argument method to get all the documents list name System.out.print ( "file to the directory: \ n-" ); for ( int I = 0; I <all.length; I ++) { // output System.out.print (all [i] + " \ the n-" ); } Of System.out.print ( "Please enter the file type of the directory requires: \ n-" ); type filetype = new type(reader.nextLine()); String [] Type = file.list (filetype); // call parameters for the file type list method to get the class file name System.out.print ( "the file type of file to the directory: \ the n-" ); for ( int I = 0; I <Type.length; I ++ ) { System.out.print(Type[i]+"\n"); } System.out.print ( "Please enter the need to cut the file name: \ the n-" ); Cut String = reader.nextLine (); // scratch file name File cutfile = new new File (Way + "\\" + Cut); // stored in the original file System.out.print ( "Please enter the file to be moved to directory: \ the n-" ); Cutway String = reader.nextLine (); // temporary directory File cutlist = new new File (cutway); // stored in the directory File newfile = new new File (cutway + "\\" + Cut); // into a new directory new file the try { newfile.createNewFile (); // create a file with the same name } the catch (IOException E) { e.printStackTrace (); } InputStream InputStream = null ; // read the original contents of the file object is created BufferedInputStream BufferedInputStream = null ; The Data String = ""; // contents of the register Writer Writer = null ; // write the new contents of the file object is created BufferedWriter BufferedWriter = null ; the try { inputStream = new FileInputStream(cutfile); BufferedInputStream = new new BufferedInputStream (inputStream); byte [] Content = new new byte [1024]; // contents stored int COUNT = 0; // record the length of the while ! ((COUNT = bufferedInputStream.read (Content, 0, 1024)) = -1 ) { Data = Data + new new String (Content, 0, COUNT); // traversed bytes into strings } writer = new FileWriter(newfile); bufferedWriter = new BufferedWriter(writer); bufferedWriter.write (Data); // string to the new file } the catch (a FileNotFoundException E1) { e1.printStackTrace (); } catch (IOException e) { e.printStackTrace (); }finally{ try { inputStream.close(); bufferedWriter.close(); writer.close(); } catch (IOException e) { e.printStackTrace (); } } cutfile.delete (); // delete the original file } }
result: