【JavaEE】——文件IO的应用

 

8e19eee2be5648b78d93fbff2488137b.png

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

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

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

目录

 

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

 二:文件的复制

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


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

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打开文件的方式

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

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)的文件所在的路径,并打印路径

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值