JavaEE:文件内容操作练习(三)

文章目录


文件内容操作练习

练习1

扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件.

java 复制代码
package javaEE.fileIO;

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

public class O {

    private static void scan(File currentFile, String key) {
        if (!currentFile.isDirectory()) {
            return;
        }
        File[] files = currentFile.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File f : files) {
            if(f.isFile()) {
                // 针对普通文件进行处理,判定文件名是否符合要求并提示用户删除
                doDelete(f,key);
            } else {
                scan(f,key);
            }
        }
    }

    private static void doDelete(File f,String key) {
        if(!f.getName().contains(key)) {
            //文件名中不包含指定的关键字
            return;
        } else {
            // 提示用户是否要删除
            Scanner scanner = new Scanner(System.in);
            System.out.println(f.getAbsolutePath() + "确认删除?Y/N");
            String choice = scanner.next();
            if(choice.equals("Y") || choice.equals("y")) {
                f.delete();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("请输入要搜索的路径");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if (!rootFile.isDirectory()) {
            System.out.println("输入的路径不存在");
            return;
        }
        System.out.println("请输入要删除的文件名字的关键字");
        String key = scanner.next();

        // 进行递归查找
        scan(rootFile, key);
    }
}

代码运行前:

代码运行后:

练习2

进行普通文件的复制.

java 复制代码
package javaEE.fileIO;

import java.io.*;
import java.util.Scanner;

public class P {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入原文件的路径:");
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);
        // 原文件需要确保路径存在,并且还要是一个文件
        if(!srcFile.isFile()) {
            System.out.println("原文件路径有误!");
            return;
        }
        System.out.println("请输入目标文件的路径");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        // 目标文件需要确保目录存在
        if(!destFile.getParentFile().isDirectory()) {
            System.out.println("目标文件的路径有误");
            return;
        }
        // try()里可以写多个对象,多个对象的构造过程使用 ; 分隔
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while (true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                // 这里一定要指定区间,因为在最后一次从文件中读数据时,不一定是1024个字节!!
                outputStream.write(buffer,0,n);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行前:

运行后:

练习3

扫描指定路径,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)

java 复制代码
package javaEE.fileIO;

import java.io.*;
import java.nio.file.Files;
import java.util.Scanner;

public class Q {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的目录");
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if (!rootFile.isDirectory()) {
            System.out.println("要搜索的路径有误!");
            return;
        }
        System.out.println("请输入要搜索的查询词:");
        String key = scanner.next();

        // 进行查询
        scan(rootFile, key);
    }

    private static void scan(File rootFile,String key) {
        if(!rootFile.isDirectory()) {
            return;
        }
        File[] files = rootFile.listFiles();
        if(files == null || files.length == 0) {
            return;
        }
        for (File f: files) {
            if(f.isFile()) {
                // 进行查询
                doSearch(f,key);
            } else {
                // 进行递归
                scan(f,key);
            }
        }
    }
    private static void doSearch(File f,String key) {
        StringBuilder stringBuilder = new StringBuilder();
        // 打开文件,读取文件内容,判定文件内容是否包含key
        try(Reader reader = new FileReader(f)) {
            char[] buffer = new char[1024];
            while (true) {
                int n = reader.read(buffer);
                if(n == -1) {
                    break;
                }
                String s = new String(buffer,0,n);
                stringBuilder.append(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(stringBuilder.indexOf(key) == -1) {
            // 没找到
            return;
        }
        // 找到了
        System.out.println("找到匹配的文件" + f.getAbsolutePath());
    }
}

运行结果

此处这里的代码逻辑,效率是非常低的,每次查询,都会涉及到大量的硬盘IO操作.

这种思路,不能适应频繁查询的场景,也不能适应目录中文件数目特别多,特别大的场景.

这种基于内容的查询,其实也是存在的,比如说搜索引擎.

搜索引擎在进行搜索的过程,也就是在文件中查找内容是否被包含的过程.

搜索出来的结果其实就是一些html文件.

这些html文件里面一定是包含你的查询词(或者和你的查询词有关联)

上图是在数以十亿,数以百亿的html中,找到了30w个结果.

搜索引擎这样的场景,不能通过上述"遍历文件"的方式实现.

其中最核心的优化,就是引入了神奇的数据结构"倒排索引".提前把所有的文件里面的内容都分析好,分析出一个文件中,包含哪些词,然后基于这个结果,得到另一份数据,每个词,都在哪些文件中包含着.

实际工作中,也会用到一些"自定制的搜索引擎".

比如,我们自己的代码中,产生大量的日志,把这些日志,导入到自己搭建的搜索引擎中(用到一些业界成熟的方案,比如ES这种),从而快速查找.

本文到这里就结束啦~

相关推荐
凤山老林2 分钟前
基于 Spring Batch 的海量数据迁移与批处理架构:分片、容错与断点续跑
java·spring boot·spring·架构·spring batch
Javatutouhouduan13 分钟前
Java初学者如何高效学习JVM?
java·jvm·java虚拟机·java面试·后端开发·java程序员·java八股文
ZJU_统一阿萨姆33 分钟前
【算子开发】全局内存访问与合并访存
java·服务器·网络·人工智能·语言模型
7177771 小时前
不止工具集成:基于 Gitee 软件工厂构建 DevSecOps 研发治理底座
java·服务器·gitee
蜡台1 小时前
Jetpack Compose 核心:状态管理 + Lazy 列表
android·kotlin·state·jetpack compose
gis开发之家1 小时前
Spring Boot 4 深度解析——参数接收大全:@RequestParam、@PathVariable、@RequestBody
java·spring boot·后端·spring
(轻舟已过万重山)1 小时前
D1 · 融合蓝图:Spring AI + 虚拟线程 + 服务网格——现代后端统一底座
java·人工智能·spring
SamDeepThinking1 小时前
警惕那些很长时间没有编写任何代码、却在设计系统的人
java·后端·架构
莫得感情 o1 小时前
并发 13 · 异步编排
java·并发
TizzyGoodhealth2 小时前
从零到一搭建Spring‑AI原生Tool‑Calling AI Agent|架构对比+完整实战源码+踩坑实录
java·ai·知识库·rag·ai agent·spring ai