首页 > 代码库 > java读取本地文件备份并解析入库
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; }
java读取本地文件备份并解析入库
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。