第14次作业

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) {
        // TODO Auto-generated method stub
        System.out.println("输入一个目录");
        Scanner reader = new Scanner(System.in); 
        String s = reader.next();
        File dir = new File(s);                        
        System.out.println("输入文件类型");
        Scanner reader2 = new Scanner(System.in); 
        String k = reader2.next();
        FileAccept con = new FileAccept(k);
        String fileList[] = dir.list(con);
        System.out.println("目录下有"+fileList.length+"个文件");
        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 = "D:\\Java.Test\\新建文件夹"+"\\"+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();                
        
    }
}

剪切前

 

剪切后

 

 

剪切到目录

 

 

 

 

posted on 2019-12-08 22:53  张九川  阅读(166)  评论(0编辑  收藏  举报

导航