java讀取本地檔案備份並解析入庫

來源:互聯網
上載者:User

標籤: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讀取本地檔案備份並解析入庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.