• 第14周作业


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

    源代码:

    TestMain.java
    package a;
    import java.util.Scanner;
    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;
    public class TestMain {
    
        public static void main(String[] args) {
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入原路径:");
            String sourcePath=scanner.nextLine();
            File f=new File(sourcePath);
            FilenameFilter file=new X("sourcePath");
            String[] names=f.list(file);
        for(String name:names) {
            System.out.println(name);
        }
        
        System.out.println("请输入后缀类型:");
        String type=scanner.nextLine();
        X targetType=new X(type);
        String[] targetTypenames=f.list(targetType);
        for(String name:targetTypenames) {
            System.out.println(name);
        }
        System.out.println("请输入要剪切的文件名:");
        String cutName=scanner.nextLine();
        File cutfile=new File(sourcePath+"//"+cutName);
        System.out.println("请输入该文件需要移动到的目标目录:");
        String targetPath=scanner.nextLine();
        File cutlist=new File(targetPath);
        File newfile=new File(targetPath+"//"+cutName);
        try {
            newfile.createNewFile();
        }
        catch(IOException e){
            System.out.println("出现异常");
        }
        
       InputStream in=null;
       BufferedInputStream bis=null;
       Writer w=null;
       BufferedWriter bw=null;
       try {
           in=new FileInputStream(cutfile);
           bis=new BufferedInputStream(in);
           byte[] b=new byte[1024];
           int count=0;
           while((count=bis.read(b,0,1024))!=-1) {
               System.out.println(new String(b));
               
           }
           w=new FileWriter(newfile);
           bw=new BufferedWriter(w);
         //  bw.write(new String(b));
           
       }
       catch(FileNotFoundException e) {
           e.printStackTrace();
       }catch(IOException e) {
           e.printStackTrace();
       }finally {
           try {
               bis.close();
               in.close();
               bw.close();
               w.close();
           }
           catch(IOException e) {
               e.printStackTrace();
           }
       }
       cutfile.delete();
        }
    
    }
    class X implements FilenameFilter{
        String type;
        X(String type){
            this.type=type;
        }
        public boolean accept (File f,String name) {
            return name.endsWith(type);
        }
    }

    运行结果:

  • 相关阅读:
    网页特殊符号HTML代码大全
    TypeScript在react项目中的实践
    koa源码阅读[3]-koa-send与它的衍生(static)
    微任务、宏任务与Event-Loop
    koa源码阅读[2]-koa-router
    koa源码阅读[1]-koa与koa-compose
    koa源码阅读[0]
    TypeScript在node项目中的实践
    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
    Javascript装饰器的妙用
  • 原文地址:https://www.cnblogs.com/GXTSTAY/p/12000779.html
Copyright © 2020-2023  润新知