项目结构
思路:想要实现读取文件增加行的信息最终要的一点就是得知道上次读文件的时候读到哪一行了,所以我们只需要建立一个能够记录上次读取行号的文件,等下次读取时按照这个行号接着往下读取就可以了。
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 {
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);
}
}
updateProperties(file, count);
return ls;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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));
}
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();
}
}
public static String getValue(String key){
return properties.getProperty(key);
}
public static void update(String key, String value) {
properties.setProperty(key, value);
try(FileOutputStream oFile = new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(path).getPath())) {
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();
}
}
}