码迷,mamicode.com
首页 > 编程语言 > 详细

java读取本地文件备份并解析入库

时间:2016-11-19 15:27:57      阅读:223      评论:0      收藏:0      [点我收藏+]

标签: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读取本地文件备份并解析入库

标签:string   结束时间   ber   blog   tin   put   nbsp   rtt   文件   

原文地址:http://www.cnblogs.com/zhaoyifan123/p/6080383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!