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

    一.题目:编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。

    package file;
    
    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.Scanner;
    
    public class file {
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            System.out.print("请输入目录");
            File f = new File(reader.nextLine());
            String[] filenames = f.list();
            System.out.print("该目录的文件为" + "
    ");
    
            for (int i = 0; i < filenames.length; i++) {
                System.out.print(filenames[i] + "
    ");
            }
            System.out.print("请输入你想查找文件的类型:");
            X typefile = new X(reader.nextLine());
            String[] filenames1 = f.list(typefile);
            System.out.print("此类型文件为");
            for (int i = 0; i < filenames1.length; i++) {
                System.out.print(filenames1[i] + "
    ");
            }
        }
    
    }
    
    class X implements FilenameFilter {
        String type;
    
        X(String type) {
            this.type = type;
        }
    
        public boolean accept(File file, String name) {
            return name.endsWith(type);
        }
    }

    (2)运行成功截图

    二.之后,将这些文件中的某一个文件剪切到另外一个目录中。

    package file;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FilenameFilter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Scanner;
    
    public class Cutfiles {
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            System.out.print("请输入目录");
            String path = reader.nextLine();
            File f = new File(path);
            String[] filenames = f.list();
            System.out.print("该目录的文件为" + "
    ");
    
            for (int i = 0; i < filenames.length; i++) {
                System.out.print(filenames[i] + "
    ");
            }
            System.out.print("请输入你想查找文件的类型:");
            X1 typefile = new X1(reader.nextLine());
            String[] filenames1 = f.list(typefile);
            System.out.print("此类型文件为");
            for (int i = 0; i < filenames1.length; i++) {
                System.out.print(filenames1[i] + "
    ");
            }
            System.out.print("请输入需要剪切的文件名:
    ");
            String cut = reader.nextLine();
            File cutfile = new File(path + "\" + cut);
            System.out.print("请输入该文件需要移动到的目录:
    ");
            String cutpath = reader.nextLine();
    
            File file1 = new File(cutpath);
            file1 = cutfile;
            File file2 = new File(cutpath + "\" + cut);
    
            // 在程序结束时删除文件1
            file1.deleteOnExit();
            try {
    
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            cutFile(file1, file2);
        }
    
        public static void cutFile(File file1, File file2) {
            FileOutputStream fileOutputStream = null;
            InputStream inputStream = null;
            byte[] bytes = new byte[1024];
            int temp = 0;
            try {
                inputStream = new FileInputStream(file1);
                fileOutputStream = new FileOutputStream(file2);
                while ((temp = inputStream.read(bytes)) != -1) {
                    fileOutputStream.write(bytes, 0, temp);
                    fileOutputStream.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
    
    class X1 implements FilenameFilter {
        String type;
    
        X1(String type) {
            this.type = type;
        }
    
        public boolean accept(File file, String name) {
            return name.endsWith(type);
        }
    }

    (2)运行成功截图

  • 相关阅读:
    NodeJS学习笔记 (7)网络服务-http-client(ok)
    NodeJS学习笔记 (6)网络服务-http-res(ok)
    NodeJS学习笔记 (5)网络服务-http-req(ok)
    NodeJS学习笔记 (4)网络服务-http(ok)
    NodeJS学习笔记 (3)域名解析-dns(ok)
    NodeJS学习笔记 (2)文件系统操作-fs(ok)
    NodeJS学习笔记 (1)资源压缩-zlib(ok)
    indexedDB介绍
    React diff机制(介绍虚拟DOM的机制)
    区间dp总结
  • 原文地址:https://www.cnblogs.com/912760869-qq/p/11991441.html
Copyright © 2011-2022 走看看