java Week 14 jobs - file

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

II. Code

FileAccept class

import java.io.*;
public class FileAccept implements FilenameFilter {
    String str = null;
    FileAccept(String s){
        str = "."+s;
    }
    public boolean accept(File dir,String name){
        return name.endsWith(str);
    }

}

The main class

Import the java.io. * ;
 Import Classes in java.util *. ;
 public  class FileDemo { 

    / ** 
     * @param args
      * / 
    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        the System.out. println ( "Please enter the directory:" ); 
        Scanner Reader = new new Scanner (System.in); 
        String s1 = reader.nextLine (); 
        file dir = new new file (s1); 
        System.out.println ( "Please enter the file type : "  );
        String s2= reader.nextLine();
        FileAccept fa = new FileAccept(s2);
        
        String fileList[] = dir.list(fa);
        
        
        for(int i=0;i<fileList.length;i++){
            System.out.println(fileList[i]);
        }
        
         System.out.println("输入要剪切的文件");
         String g = reader.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);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
                w.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      }
}

 

III. Screenshot

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/19980712mj/p/11997000.html