1. 程式人生 > >讀取TXT並記錄行數 下次讀取記錄的行數

讀取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/