【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);
        }
    }
}
相关推荐
彭于晏689几秒前
Android广播
android·java·开发语言
弱冠少年26 分钟前
websockets库使用(基于Python)
开发语言·python·numpy
长天一色27 分钟前
C语言日志类库 zlog 使用指南(第五章 配置文件)
c语言·开发语言
一般清意味……39 分钟前
快速上手C语言【上】(非常详细!!!)
c语言·开发语言
卑微求AC39 分钟前
(C语言贪吃蛇)16.贪吃蛇食物位置随机(完结撒花)
linux·c语言·开发语言·嵌入式·c语言贪吃蛇
技术无疆1 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
羊小猪~~1 小时前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
爱上语文1 小时前
Java LeetCode每日一题
java·开发语言·leetcode
qq_273900231 小时前
解析TMalign文本文件中的转换矩阵
python·生物信息学