zoukankan      html  css  js  c++  java
  • 第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);
        }
    }

    运行结果:

  • 相关阅读:
    批处理手动设置电脑的ip
    用shell脚本生成日志文件
    编译小米mini openwrt
    Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明
    swagger注释API详细说明
    com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95)
    在Java中如何高效判断数组中是否包含某个元素
    阿里云服务器端口开放对外访问权限
    macos 安装telnet命令
    在 Docker 上配置 Oracle
  • 原文地址:https://www.cnblogs.com/GXTSTAY/p/12000779.html
Copyright © 2011-2022 走看看