標籤:string 結束時間 ber blog tin put nbsp rtt 檔案
//從設定檔中擷取檔案路徑String filePath = Global.getConfig("filePath", "log-resolve.properties");String copyFilePath = Global.getConfig("copyFilePath", "log-resolve.properties");try {String encoding = "utf-8";File file = new File(filePath);//判斷檔案是否存在if(file.isFile()&&file.exists()){ //step1 備份檔案,清理原檔案 copyFile(filePath, copyFilePath); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write(""); bw.close(); //step2 讀取備份檔案,並解析入庫 File fileCopy = new File(copyFilePath); InputStreamReader read = new InputStreamReader(new FileInputStream(fileCopy),encoding); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = ""; while((lineTxt=bufferedReader.readLine())!=null){ System.out.println(lineTxt); //截取字串 //截取檔案編號 String fileNumber = lineTxt.substring(0, lineTxt.indexOf(" ")); //截取開始時間 String str = lineTxt.substring(0, lineTxt.lastIndexOf(" ")-1); String startTime = str.substring(str.lastIndexOf(" ")+1); //截取檔案名稱 String fileName = str.substring(str.indexOf(" ")+2, str.lastIndexOf(" ")-1); //截取結束時間 String endTime = lineTxt.substring(lineTxt.lastIndexOf(" ")+1); OneWayRunLog oneWayRunLog = new OneWayRunLog(); oneWayRunLog.setFileNumber(fileNumber); oneWayRunLog.setFileName(fileName); oneWayRunLog.setStartTime(startTime); oneWayRunLog.setEndTime(endTime); oneWayRunLogService.save(oneWayRunLog); } read.close();}else{ System.out.println("找不到指定的檔案");}/** * 檔案備份 * */public static int copyFile(String src, String dst) { try { int len = 0; byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dst); while ( (len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (IOException e) { System.out.println(e); return -1; } return 0; }
java讀取本地檔案備份並解析入庫