2. Batch operation file manager

In daily work, batches of operating system files are often encountered. Usually, the operation of batch files can only be completed manually and repeatedly, which is very time-consuming and laborious. This case requires writing a file manager to implement batch operations on files. The specific functional requirements of the file manager are as follows:

(1) The user enters the command 1, which means "specify keywords to search for files". At this time, the user needs to input the directory and keywords to be searched. Absolute paths are displayed.

(2) The user enters command 2, which stands for "retrieve files with a specified suffix name". At this time, the user needs to input the search directory and suffix name (multiple suffix names are separated by commas), and the system will retrieve the specified suffix name in the directory specified by the user. file and display its absolute path.

(3) The user enters the command 3, which stands for "copy file/directory". At this time, the user needs to input the source directory and the target directory. After the program is executed, the contents of the source directory will be copied to the target directory.

(4) The user enters the command 4, which means "delete file/directory". At this time, the user needs to input the file directory to be deleted. After the program is executed, the directory and all the contents under the directory will be deleted.

(5) The user enters the command 5, which means "exit", that is, to exit the system.

main menu

package pack;
public class menu {
    public static void ShowMenu(){
        System.out.println("----欢迎使用文件管理器-----");
        System.out.println("----按要求选择功能---------");
        System.out.println("----1.指定关键字检索文件---");
        System.out.println("----2.指定后缀名检索文件---");
        System.out.println("----3.复制文件/目录-------");
        System.out.println("----4.删除文件/目录-------");
        System.out.println("----5.退出---------------");
    }
}

main function

package pack;

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

public class fileManager {

    public static void main(String[] args) throws InterruptedException {
        while(true){
            menu.ShowMenu();
            Scanner sc=new Scanner(System.in);
            String s=sc.nextLine();
            switch(s){
                case "1":
                {
                    System.out.println("请输入你要检索的目录");
                    String dir=sc.nextLine();
                    System.out.println("请输入你要搜索的关键字");
                    String key=sc.nextLine();
                    //findWithKey fi=new findWithKey();
                    findWithKey.findkey(new File(dir),key);
                    System.out.println("按照关键字查找完成!");
                    findWithKey.showNum();
                    Thread.currentThread().sleep(3000);
                    System.out.println("\n\n\n\n");//实现清屏

                }
                break;
                case "2":{
                    System.out.println("请输入检索的目录");
                    String dir1=sc.nextLine();
                    System.out.println("请输入后缀名,不同的后缀用逗号隔开");
                    String lastname=sc.nextLine();
                    String [] sublastname=lastname.split(",");//将获取到的字符串分割成若干个小的后缀
                    findWithName.findname(new File(dir1),sublastname);
                    System.out.println("按照后缀查找完成!");
                    findWithName.showNum1();
                    Thread.currentThread().sleep(3000);
                    System.out.println("\n\n\n\n");//实现清屏
                }
                break;
                case "3":{
                    System.out.println("请输入源文件夹");//D:\ideap\测试
                    String first=sc.nextLine();
                    System.out.println("请输入目标文件夹");//D:\测试
                    String second=sc.nextLine();
                    copyFile.copyFloder(first,second);
                    System.out.println("复制完成!");
                    Thread.currentThread().sleep(3000);
                    System.out.println("\n\n\n\n");//实现清屏
                }
                break;
                case "4":
                    {
                    System.out.println("请输入你要删除的目录!");
                    String dir=sc.nextLine();
                    deleteFile.deleteDir(new File(dir));
                    System.out.println("删除完成!");
                    Thread.currentThread().sleep(3000);
                    System.out.println("\n\n\n\n");//实现清屏
                    }
                    break;
                case "5":
                    return;
                default:
                    System.out.println("---选择出错,3秒后请重新选择功能---");
                    Thread.currentThread().sleep(3000);
                    System.out.println("\n\n\n\n");//实现清屏

            }
        }
    }
}

Function 1: Keyword Search

package pack;

import java.io.File;

/*
用户输入指令1,代表“指定关键字检索文件”,此时需要用户输入检索的目录和关键字,
系统在用户指定的目录下检索出文件名中包含关键字的文件,并将其绝对路径展示出来。
 */
public class findWithKey {
    public  static int num=0;
    public  static void findkey(File dir,String key){
        if(dir.exists()){
            File[] file =dir.listFiles();
            for (File file1:file
                 ) {
                if(file1.isDirectory()){
                    findkey(file1,key);
                }
                else{
                    String s=file1.getName();
                    if(s.contains(key)){
                        num++;
                        System.out.println(file1.getName());
                        System.out.println(file1.getAbsolutePath());
                    }
                }
            }
        }

    }
    public static void showNum(){
        System.out.println("本次查找一共查找到"+num+"个文件");//为什么会打印两次
    }

}

function 2

package pack;

import java.io.File;

/*
用户输入指令2,代表“指定后缀名检索文件”,此时需要用户输入检索的目录和后缀名(多个后缀名用逗号分隔),
系统在用户指定的目录下检索出指定后缀名的文件,并将其绝对路径展示出来。
 */
public class findWithName {
    public  static int num1=0;

    public static void findname(File file,String[] s){
        if(file.exists()){
            File[] files=file.listFiles();//获取目录中的所有File对象,方面遍历
            for (File f:files
                 ) {
                if(f.isDirectory()){
                    findname(f,s);
                }
                else{
                    for (int i = 0; i <s.length ; i++) {
                        if(f.getAbsolutePath().toLowerCase().endsWith(s[i])){
                            num1++;
                            System.out.println(f.getAbsolutePath());
                        };
                    }
                }
            }
        }
        else{
            System.out.println("目录不存在,请输入正确的目录");
            return;
        }

    }
    public static void showNum1()
    {
        System.out.println("本次查找一共查找到"+num1+"个文件");//为什么会打印两次
    }
}

function 3

package pack;

import java.io.*;

/*
(3)用户输入指令3,代表“复制文件/目录”,此时需要用户输入源目录和目标目录,
     程序执行后会将源目录下的内容复制到目标目录下。
 */
public  class copyFile {
    public static void copyFloder(String srcFloder, String destFloder) {
        File f1 = new File(srcFloder);
        File f2 = new File(destFloder);
        if (!f1.exists()) {
            System.out.println("源文件夹不存在!");
            return;
        }
        if (srcFloder.equals(destFloder)) {
            System.out.println("目标文件夹与源文件夹在同一目录,复制无效");
            return;
        }
        File[] files = f1.listFiles();
        if (!f2.exists())
            f2.mkdirs();
        if (files == null) {
            return;
        }
        for (File eachFile : files) {
            if (eachFile.isDirectory()) {
                String subSrcFloder = new String(eachFile.getAbsolutePath());
                String subDestFloder = new String(destFloder + "/" + eachFile.getName());
                copyFloder(subSrcFloder, subDestFloder);
            } else if (eachFile.isFile()) {
                FileInputStream fis = null;
                FileOutputStream fos = null;
                byte[] bytes = new byte[1024];
                int len;
                try {
                    fis = new FileInputStream(eachFile);
                    fos = new FileOutputStream(destFloder + "/" + eachFile.getName());
                    while ((len = fis.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fos.close();
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }
}

function 4

package pack;

import java.io.File;

/*
用户输入指令4,代表“删除文件/目录”,此时需要用户输入需要删除掉的文件目录,
程序执行后会将目录以及目录下的内容全部删除。
 */
public class deleteFile {
    public static void deleteDir(File dir){
        if(dir.exists()){           //判断文件是否存在
            File[] file=dir.listFiles();//如果文件存在,那么我们将所有文件取出来
            for (File file1:file
                 ) {
                if(file1.isDirectory()){//判断是文件还是目录,如果是目录则递归这个方法
                    deleteDir(file1);
                }
                else{
                    file1.delete();//如果是文件则直接删除
                }
            }
            dir.delete();//最后把目录删除
        }
    }

}

Guess you like

Origin blog.csdn.net/qq_47701945/article/details/122517322