迟到的第14周作业

题目:编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。之后,将这些文件中的某一个文件剪切到另外一个目录中。

package fourteen;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Scanner;

/**
 * @author WH
 * @project 作业
 * @package fourteen
 * @date 2019/12/8 11:56
 */
class FileAccept implements FilenameFilter{
    String str=null;
    FileAccept(String s){
        str="."+s;
    }
    @Override
    public boolean accept(File dir, String name) {
        return name.endsWith(str);
    }
}
/**
 * @author WH
 */
public class FileTest {
    public static void main(String[] args) {
        System.out.println("输入路径和类型:");
        Scanner s=new Scanner(System.in);
        String cata=s.nextLine();
        String type=s.nextLine();
        File dir=new File(cata);
        FileAccept accept=new FileAccept(type);
        String[] file =dir.list(accept);
        assert file != null;
        for (String t:file){
            System.out.println(t);
        }
    }
}
package fourteen;

import exercise.Estring;

import java.io.*;
import java.util.Scanner;

/**
 * @author WH
 * @project 作业
 * @package fourteen
 * @date 2019/12/8 12:25
 */
public class FileCopyTest {
    public static void main(String[] args) {
        System.out.println("输入文件路径和移动路径:");
        Scanner s=new Scanner(System.in);
        String src=s.nextLine();
        String dest=s.nextLine();
        InputStream is=null;
        OutputStream os=null;
        try{
            is=new FileInputStream(src);
            os=new FileOutputStream(dest);
            byte[] flush=new byte[1024];
            int len=-1;
            while((len=is.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
            System.out.println("剪切成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                assert os != null;
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

 

 

posted @ 2019-12-08 13:23  Nicholas-Wang  阅读(86)  评论(0编辑  收藏  举报