2.批量操作文件管理器

在日常工作中,经常会遇到批量操作系统文件的事情,通常情况下,只能手动重复的完成批量文件的操作,这样很是费时费力。本案例要求编写一个文件管理器,实现文件的批量操作。文件管理器具体功能要求如下:

(1)用户输入指令1,代表“指定关键字检索文件”,此时需要用户输入检索的目录和关键字,系统在用户指定的目录下检索出文件名中包含关键字的文件,并将其绝对路径展示出来。

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

(3)用户输入指令3,代表“复制文件/目录”,此时需要用户输入源目录和目标目录,程序执行后会将源目录下的内容复制到目标目录下。

(4)用户输入指令4,代表“删除文件/目录”,此时需要用户输入需要删除掉的文件目录,程序执行后会将目录以及目录下的内容全部删除。

(5)用户输入指令5,代表“退出”,即退出该系统。

主菜单

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.退出---------------");
    }
}

主函数

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");//实现清屏

            }
        }
    }
}

功能1:关键字查找

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+"个文件");//为什么会打印两次
    }

}

功能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+"个文件");//为什么会打印两次
    }
}

功能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();
                    }
                }

            }
        }
    }
}

功能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();//最后把目录删除
        }
    }

}

  • 4
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值