读取TXT并记录行数 下次读取记录的行数

这是主类
/**
* 读取文件具体操作
* @author Thinkpad
*
*/
public class ReadFile
{
private static SimpleDateFormat logTimeSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //日志时间格式

private static String middle_file_path = ""; //中间文件路径
private static String read_process_path = ""; //读取进度文件路径

//初始化配置文件
public static void initConfig() throws Exception{
InputStream in = null;
try{
File file = new File(ReadFile.class.getResource("/config.properties").getFile().replaceAll("%20", " "));
in = new FileInputStream(file);
Properties prop = new Properties();
prop.load(in);

middle_file_path = prop.getProperty("middle_file_path");
read_process_path = prop.getProperty("read_process_path");
} catch(Exception e) {
throw e;
}finally{
if(in != null){
in.close();
}
}
}

public static String readMiddleFile() throws Exception
{
System.out.println("开始读取【能见度中间文件】: " + logTimeSDF.format(System.currentTimeMillis()));

try{
if("".equals(middle_file_path) || "".equals(read_process_path)){
initConfig();
}

File fileDir = new File(middle_file_path); //中间文件目录
if(fileDir.exists() && fileDir.isDirectory()){ //中间文件是否存在
File[] fileList = fileDir.listFiles();
if(fileList.length > 0){ //有中间文件才启动读取流程
//第一步、读取进度文件
BufferedReader in = null;
//读取入库文件
File mfp = new File(read_process_path);
if(!mfp.exists()){
mfp.mkdir();
}

Map<String, String> readProcessParams = new LinkedHashMap<>(); //文件读取进度参数
File processFile = new File(read_process_path + "/ReadMiddleFileLog.txt");

if(processFile.exists()){ //文件存在,读取进度信息
try{
in = new BufferedReader(new InputStreamReader(new FileInputStream(processFile)));
String lineText = ""; //每行内容
while( (lineText = in.readLine()) != null ){ //读到结束
lineText = lineText.trim();
String[] processInfo = lineText.split("【PARAMS】");
String fileName = processInfo[0];
readProcessParams.put(fileName, processInfo[1]);
}
}catch(Exception e){
throw e;
}finally {
if(in != null){
in.close(); in = null;
}
}
}else{
processFile.createNewFile(); //创建文件
}

//第二步、循环读取目录下中间结果文件
for(int i = 0, cc = fileList.length; i < cc; i ++){
File file = fileList[i];
String fileName = file.getName();

if(file.isFile() && fileName.indexOf(".txt") != -1){ //只读取txt文件
try{
String processInfo = readProcessParams.get(fileName); //该文件读取进度信息
int readLineNo = 0; //上次读取行号,新文件从第一行开始读
if(processInfo != null){ //该文件已经读取
String[] params = processInfo.split("【PARAM】");
readLineNo = Integer.parseInt(params[0]); //上次读取行号
}

in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
int lineNo = 0; //读取第几行
String lineText = ""; //每行内容
while( (lineText = in.readLine()) != null ){
lineNo ++;
//如果当前行数大于上次读取行数,获取该行数据
if(lineNo > readLineNo){
lineText = lineText.trim();
//保存内容,在这入库
System.out.println(lineNo + ": " + lineText);
}
}

if(readLineNo == 0 || lineNo > readLineNo ){ //有读取新记录
String newProcessInfo = lineNo + "【PARAM】"; //新的读取进度信息
readProcessParams.put(fileName, newProcessInfo); //放入进度集合,全部读完后存入进度文件
}/*else{
集合中原记录不变,和新进度一起保存到进度文件中
}*/
}catch(Exception e){
throw e;
}finally {
if(in != null){in.close(); in = null;}
}

}
}

//第三步、更新进度文件
OutputStream out = null;
try{
out = new FileOutputStream(processFile);
StringBuilder precessInfoStr = new StringBuilder("");
for(Entry<String, String> entry : readProcessParams.entrySet()){
String fileName = entry.getKey();
String processInf0 = entry.getValue();
precessInfoStr.append(fileName + "【PARAMS】" + processInf0 + /*换行*/"\r\n");
}
out.write(precessInfoStr.toString().getBytes("utf-8")); //写入文件
}catch(Exception e){
throw e;
}finally {
if(out != null){
out.close(); out = null;
}
}

}
}
}catch(Exception e){
throw e;
}

return "success";
}
}


/**
* 读取线程
* @author Thinkpad
*
*/
public class ReadRunnable implements Runnable
{
public void run()
{
try{
ReadFile.readMiddleFile();
}catch(Exception e){
System.out.println("【能见度中间文件】读取异常:" + e);
}catch(Error e){
System.out.println("【能见度中间文件】读取错误:" + e);
}
}
}

/**
* 读取能见度中间结果文件入口
* @author Thinkpad
*
*/
public class MainRead
{
public static void main(String[] args)
{
ReadRunnable readRun = new ReadRunnable();
ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
schedule.scheduleWithFixedDelay(readRun, 5, 20, TimeUnit.SECONDS); //10秒后开始执行,每8秒执行异常
}
}

#中间文件路径
middle_file_path = C:/Users/Thinkpad/Desktop/Visibility MiddleFile Read/middle file/
#读取进度文件路径
read_process_path = C:/Users/Thinkpad/Desktop/Visibility MiddleFile Read/ReadMiddle Log/

猜你喜欢

转载自www.cnblogs.com/heisenbergXu/p/10100924.html
今日推荐