1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package com.xxx.spmacore.common.utils;
import com.xxx.spmacore.soa.mapper.AccountMapper; import com.xxx.spmacore.soa.mapper.BankInfoMapper; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
@Component @Slf4j public class CSVUtils {
@Autowired private BankInfoMapper bankInfoMapper;
@Autowired private AccountMapper accountMapper;
private static final int SOA_BANK = 1; private static final int SOA_ACCOUNT = 2;
public void getFileName(File f) {
File[] file = f.listFiles(); for (File file2 : file) {
String fileName = file2.getName(); if(!file2.isDirectory()){
long start = System.currentTimeMillis();
if(fileName.indexOf("soa_bank") != -1){ insertData(file2,SOA_BANK); }else if(fileName.indexOf("soa_account") != -1){ insertData(file2,SOA_ACCOUNT); } log.info("文件{}导入完成,用时{}ms",file2.getPath(),(System.currentTimeMillis()-start)); } if(file2.isDirectory()) { getFileName(file2); } } }
public List readCSV(BufferedReader reader){ try { ArrayList<String> list = new ArrayList<String>(); String line = null; for(int i=0; i < 1000; i++) { line = reader.readLine();
if(line == null){ break; }
line = "'"+line.replaceAll("︴","','")+"'";
String regex1 = "['][0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}[']"; Pattern pattern1 = Pattern.compile(regex1); Matcher m1 = pattern1.matcher(line); line = line.replaceAll(regex1 ,"︴"); while(m1.find()){ line = line.replaceFirst("︴"," to_date ( "+m1.group()+", 'YYYY-MM-DD HH24:MI:SS' )"); }
String regex2 = "['][0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}[ ][0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}[']"; Pattern pattern2 = Pattern.compile(regex2); Matcher m2 = pattern2.matcher(line); line = line.replaceAll(regex2 ,"︴"); while(m2.find()){ line = line.replaceFirst("︴"," to_date ( "+m2.group()+", 'YYYY-MM-DD HH24:MI:SS' )"); }
list.add(line); } return list; } catch (Exception e) { log.error("文件:{}读取信息出错"); try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } e.printStackTrace(); } return null; }
public void insertData(File file,int type) {
String head = null; List<String> list = null; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); head = reader.readLine();
if(head == null){ log.info("文件{}内容为空!!!",file.getPath()); return; } else { head = head.replaceAll("︴",","); }
while ((list = readCSV(reader)).size() != 0){
switch (type){ case 1:{ bankInfoMapper.insertListData(head,list); } break; case 2:{ accountMapper.insertListData(head,list); } break; }
log.info("文件{}导入中--- +{}行",file.getPath(),list.size()); } } catch (IOException e) { log.info("***文件{}导入数据库出现异常***",file.getPath()); e.printStackTrace(); }finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
}
|