Fourteenth java jobs

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.

Source:

package cn.edu.ccut.po7;
import java.io.*;
import java.util.*;

class F implements FilenameFilter{
    String type;
     F(String type){
         this.type=type;
   }
    public boolean accept(File file,String name){
        return name.endsWith(type);
     }
}
 public class Test {

  public static void main(String[] args) {
     Scanner reader=new Scanner(System.in);
      System.out.println("Please enter the path:" ); 
      String P = reader.nextLine (); 
      File File = new new File (P); 
      String filename [] = File.List for ();
      for (String name: filename) { 
        System.out.println (name); 
       } 
      System.out.println ( "file type input query:" ); 
       F. filew = new new F. (reader.nextLine ()); 
      String filename1 [] = File.List for (filew); 
     for (String name : filename1) { 
            System.out.println (name); 
        } 
             System.out.println ( "Please enter a cut file name:"); 
             String Cut = reader.nextLine (); 
            File cutfile = new new File (the p-+ "\\" + Cut); 
            System.out.println ( "Please enter the file needs to be moved to the directory:" ); 
             String cutway = Reader .nextLine (); 
            File cutlist = new new File (cutway); 
            File newfile = new new File (cutway + "\\" + Cut);
              the try { 
               newfile.createNewFile (); 
           } the catch (IOException E) { 
                e.printStackTrace (); 
             }
             InputStream inputStream = null;
             BufferedInputStream bufferedInputStream = null;
            String filedata="";
            Writer writer = null;
             BufferedWriter bufferedWriter = null;
            try {
                 inputStream = new FileInputStream(cutfile);
               bufferedInputStream = new BufferedInputStream(inputStream);
                 byte[] b = new byte[1024];
               int count = 0;
                while((count = bufferedInputStream.read(b, 0, 1024))!=-1){
                   filedata=filedata+new String(b, 0, count);
               }
                 writer = new FileWriter(newfile);
               bufferedWriter = new BufferedWriter(writer);
                 bufferedWriter.write(filedata);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
            }finally{
                 try {
                     bufferedInputStream.close();
                    inputStream.close();
                     bufferedWriter.close();
                    writer.close();
                } catch (IOException e) {
                 e.printStackTrace();
               }
             }
            cutfile.delete();
   }
} 

Run shot

Before cutting the original file directory

After shearing comparison chart

Guess you like

Origin www.cnblogs.com/jwwy/p/11999583.html