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

//从配置文件中获取文件路径
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;
  }

 

转载于:https://www.cnblogs.com/zhaoyifan123/p/6080383.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值