zoukankan      html  css  js  c++  java
  • 迟到的第14周作业

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

    代码

    import java.util.*;
    import java.io.*;
    class FileAccept implements FilenameFilter{
        String type;
        FileAccept(String type){
            this.type = type;
        }
        public boolean accept(File Fname, String name) {
            return name.endsWith(type);
        }
        
    }
    public class z {
        public static void main(String[] args) {
            System.out.println("请输入一个你心仪的目录:");
            Scanner r = new Scanner(System.in); 
            String s = r.next();
            File Fname = new File(s);                        
            System.out.println("输入文件类型");
            Scanner r1 = new Scanner(System.in); 
            String k = r1.next();
            FileAccept count = new FileAccept(k);//声明FileAccept类型的对象->创建内存空间
            String fileList[] = Fname.list(count);//创建的文件类型对象存放在文件链表集合中***
            System.out.println(s+"目录下有"+fileList.length+"个文件");
            for(int i =0;i<fileList.length;i++) {
                System.out.println(fileList[i]);
            }        
            System.out.println("输入要剪切的文件");
            Scanner r2 = new Scanner(System.in);        
            String g = r2.next();    //输入的要剪切的文件名
            String f = s+"\"+g;        //开局输入的目录++要剪切的文件名
            File Fname1 = new File(f);
            String FilePath = "D:\helloword\wangbadan"+"\"+g;    //从当前文件夹(helloword)中输入要剪切的文件名(g)放到指定的文件夹中(wangbadan)**
            try(BufferedReader close1 = new BufferedReader(new FileReader(f));//自动关闭资源写法
                BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath));    
                ) {        
                String line = null;//线程
                while((line=close1.readLine())!=null) {
                    System.out.println(line);
                    writer.write(line);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("被剪切的"+g+"文件"+"已经被执行完毕");
            Fname1.delete();                //删除文件
        }
    }

    运行结果

  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/12-abc/p/11996549.html
Copyright © 2011-2022 走看看