【JavaEE】——文件IO的应用

阿华代码,不是逆风,就是我疯

你们的点赞收藏是我前进最大的动力!!

希望本文内容能够帮助到你!!

目录

一:文件的搜索(面试高频)

二:文件的复制

三:查询文本内容中包含"word"的文件


一:文件的搜索(面试高频)

java 复制代码
import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-05
 * Time: 13:42
 */
public class IODemon15 {
    //用代码实现,查询文件这样一个功能
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要查询的文件名");
        String fileName = scanner.next();
        System.out.println("请输入查询的目录");
        String rootPath = scanner.next();
        //这一步卡住了,创建一个文件对象
        File rootFile = new File(rootPath);
        //如果当前搜索的目录是一个文件或者是一个空目录那么报错
        if (!rootFile.isDirectory()){
            System.out.println("当前的输入有误");
        }
        //3:参数rootFile是搜索起始的文件,fileName是要找的文件
        scnnDic(rootFile,fileName);


    }
    private static void scnnDic(File rootFile , String fileName){
        //1:把当前文件的子目录全都列出来
        File[] files = rootFile.listFiles();
        //2:如果当前文件为空则返回
        if(files == null){
            return;
        }
        //3:不为空则遍历当前文件,看该文件下有哪些文件
        for(File file : files){
            System.out.println("当前遍历到的目录是:"+file.getAbsolutePath());
            if (file.isFile()){
                if (file.getName().equals(fileName)){
                    System.out.println("找到了符合要求的文件" + file.getAbsolutePath());

                }
            }else if (file.isDirectory()){
                scnnDic(file,fileName);
            }else{
                //这里暂时不用写什么东西
            }
        }

    }

}

二:文件的复制

用到了InputStream和OutputStream打开文件的方式

下述复制是二进制复制,可以复制任何照片和文件

java 复制代码
import java.io.*;
import java.nio.file.Files;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-05
 * Time: 16:10
 */
public class IODemo16 {
    //复制一个源文件
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要复制的源文件的所在路径");
        String scrPath = scanner.next();
        System.out.println("请输入复制后的文件目标所在的路径");
        String desPath = scanner.next();

        //1:判断源文件是否存在
        File scrFile = new File(scrPath);
        if (!scrFile.exists()){
            System.out.println("你要找的源文件不存在");
        }
        /*
        * 目标文件的父目录存在即可*/
        File desFile = new File(desPath);
        if (!desFile.getParentFile().exists()){
            System.out.println("目标路径有误");
        }

        /*
        * 分别以读、写的方式打开文件*/
        try(InputStream inputStream = Files.newInputStream(scrFile.toPath());
            OutputStream outputStream = new FileOutputStream(desFile)){
            while(true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if (n == -1){
                    break;
                }
                outputStream.write(buffer,0,n);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}

三:查询文本内容中包含"word"的文件

在指定文件下查询,包含输入的关键字(word)的文件所在的路径,并打印路径

java 复制代码
import jdk.internal.util.xml.impl.Input;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-05
 * Time: 18:47
 */
public class IODemo17 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入在哪个文件下查询(输入路径)");
        String rootPath = scanner.next();
        System.out.println("请输入你要查询的关键字word(查哪些文件中内容包含关键字word)");
        String word = scanner.next();

        //构造文件对象
        File file = new File(rootPath);

        //判断该文件对象是否是一个目录
        if (!file.isDirectory()){
            System.out.println("输入的路径有误");
            return;
        }
        //是一个目录在这个目录中在进行查找//scan扫描
        scanDir(file,word);
    }

    private static void scanDir(File file , String word){
        File[] files = file.listFiles();
        //files数组为null返回,说明递归到最深最里面的文件了
        if (files == null){
            return;
        }
        //遍历该文件中所包含的所有文件
        for(File f : files){
            if (f.isFile()){
                //写一个方法,来判断这个f文件内容中是否包含关键字word
                searchInFile(f,word);
            }else if (f.isDirectory()){
                //继续往下递归
                scanDir(f,word);
            }else {
                //不用写
            }
        }
    }

    private static void searchInFile(File f , String word){
        //用读的方式打开文件
        // 用文件构造的输入流读操作
        try(InputStream inputStream = new FileInputStream(f)){
            StringBuffer stringBuffer = new StringBuffer();
            while(true){
                //一次读取多个字节
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);//读到的放到数组,n为字节数量
                if (n == -1){
                    //说明文件内容读完了,那就跳出循环,非return
                    break;
                }
                //用String字符串接收一下
                String s = new String(buffer , 0 , n);
                //如果一次读不完,我们就用字符串进行拼接
                stringBuffer.append(s);
            }
            if (stringBuffer.indexOf(word) == -1){
                //返回-1的话就是没匹配到这个word关键字
                return;
            }
            //没返回-1就说明在这个文件内容中找到匹配上了word
            System.out.println("找到了,所含" + word + "的文件路径是" + f.getAbsolutePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
相关推荐
冷雨夜中漫步5 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴6 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再6 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
m0_736919107 小时前
C++代码风格检查工具
开发语言·c++·算法
喵手7 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934737 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy8 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖8 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472469 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
肖永威9 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos