【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);
        }
    }
}
相关推荐
Tanecious.25 分钟前
C++--红黑树
开发语言·c++
Top`29 分钟前
Java 泛型 (Generics)
java·开发语言·windows
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ1 小时前
如何使用Java WebSocket API实现客户端和服务器端的通信?
java·开发语言·websocket
Shartin1 小时前
Can201-Introduction to Networking: Application Layer应用层
服务器·开发语言·php
cooldream20092 小时前
Python 包管理新时代:深入了解 `uv` 的使用与实践
python·uv·包管理器
之歆2 小时前
Python-魔术方法-创建、初始化与销毁-hash-bool-可视化-运算符重载-容器和大小-可调用对象-上下文管理-反射-描述器-二分-学习笔记
笔记·python·学习
胖达不服输2 小时前
「日拱一码」025 机器学习——评价指标
人工智能·python·机器学习·评价指标
共享家95272 小时前
linux_线程概念
linux·开发语言·jvm
apihz2 小时前
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
android·服务器·开发语言·网络·数据库·网络协议·tcp/ip
brave_zhao3 小时前
JavaBeanUtils javaBean转map, 实体类转map,实体集合转List<Map>
linux·windows·python