【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);
        }
    }
}
相关推荐
Drone_xjw8 小时前
从 GDB 到 CDB:C/C++ 程序调试的两把“手术刀”
c语言·开发语言·c++
SL-staff9 小时前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
聪明的一休丶9 小时前
VLLM v0.24.0 版本深度解析:新引擎、新架构与大规模服务全家桶升级
python·架构·vllm
2zcode10 小时前
基于MATLAB图像处理的饮料瓶灌装液位检测系统设计与实现
开发语言·图像处理·matlab
万亿少女的梦16810 小时前
基于Python的高考志愿填报辅助系统设计与实现
java·spring boot·python·mysql·vue
必须得开心呀11 小时前
QT解决中文乱码问题
开发语言·qt
闲猫12 小时前
Python FastAPI + SQLAlchemy 入门教程:从零搭建你的第一个 Web 应用
前端·python·fastapi
这不小天嘛12 小时前
JAVA八股——redis篇
java·开发语言·redis
逝水无殇13 小时前
C# 字符串(String)详解
开发语言·后端·c#
精明的身影14 小时前
C++自学之路1:Hello world
开发语言·c++