java实现读取文件增加行的信息

项目结构

在这里插入图片描述

思路:想要实现读取文件增加行的信息最终要的一点就是得知道上次读文件的时候读到哪一行了,所以我们只需要建立一个能够记录上次读取行号的文件,等下次读取时按照这个行号接着往下读取就可以了。

1、properties文件用于存储文件的最后修改时间和最后读取文件的行号。
#文件的最后修改时间
file.lastModifyTime=1621670224454
#文件最后读取的行号
file.lineNum=7
2、reader.txt为被读取文件
3、ReadFile.java为读取文件增加行的信息的主要类
package com.excel.com;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ReadFile {

    /**
     * 读取文件增加的行信息
     * @param filePath
     * @return
     */
    public static List<String> content(String filePath) {

        File file = new File(filePath);
        //用于记录文件读取的内容
        ArrayList<String> ls = new ArrayList<>();
        if (!file.exists()) {//判断文件是否存在
            return ls;
        }


        if (isModify(file)) {//判断文件是否发生修改,如果未发生修改就返回
            return ls;
        }
        //获取上次读取的行号
        int lineNum = Integer.parseInt(PropertiesUtil.getValue("file.lineNum")==null?"0":PropertiesUtil.getValue("file.lineNum"));

        try (FileReader fileReader = new FileReader(file);
             LineNumberReader lineReader = new LineNumberReader(fileReader)) {
            String line;
            int count = 0;
            while ((line = lineReader.readLine()) != null) {
                count++;
                if (count > lineNum) {
                    ls.add(line);
                }
            }

            //更新properties信息
            updateProperties(file, count);
            return ls;
        } catch (Exception e) {
           throw new RuntimeException(e);
        }
    }


    /**
     * 更新配置文件的信息
     * @param file
     * @param count
     */
    private static void updateProperties(File file, int count) {
        long l = file.lastModified();
        PropertiesUtil.update("file.lastModifyTime", Long.toString(l));
        PropertiesUtil.update("file.lineNum", Integer.toString(count));
    }

    /**
     * 检查文件是否发生修改
     * @param file
     * @return
     */
    private static boolean isModify( File file) {
        String value = PropertiesUtil.getValue("file.lastModifyTime");
        long modifyTime = Long.parseLong(value);
        return modifyTime == file.lastModified();
    }


    /**
     * 清空行号游标
     */
    public static void  clear(){
        PropertiesUtil.update("file.lineNum", String.valueOf(0));
    }



    public static void main(String[] args) {
        String path = Thread.currentThread().getContextClassLoader().getResource("reader.txt").getPath();
        content(path).forEach(System.out::println);

    }
}

4、PropertiesUtil.java操作配置文件按的工具类
package com.excel.com;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

public class PropertiesUtil {
    private static  final Properties properties;
    private static final String path = "FileReader.properties";

    static {
        properties = new Properties();
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取value
     * @param key
     * @return
     */
    public static String getValue(String key){
        return properties.getProperty(key);
    }
    /**
     * 修改或者新增key
     * @param key
     * @param value
     */
    public static void update(String key, String value) {
        properties.setProperty(key, value);
        try(FileOutputStream oFile = new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(path).getPath())) {
            //将Properties中的属性列表(键和元素对)写入输出流
            properties.store(oFile, "update");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        String str = Long.toString(new Date().getTime());
        update("file.lastModifyTime",str);

        System.out.println(str);
        String value = getValue("file.lastModifyTime");

        System.out.println(value);




        try {
            System.out.println(Thread.currentThread().getContextClassLoader().getResource(path).getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值