- public static void specialChar(String filePath,int starRow) throws Exception {
- BufferedReader br = null;
- File f = new File(filePath);
- String fileName = f.getName();
- if (!fileName.substring(fileName.indexOf(".") + 1).equals("csv")) {
- throw new Exception(filePath + "不是一个CSV文件");
- }
- File file = new File(StringUtil.replace(f.getPath(), "csv", "txt"));
- FileWriter filewriter = null;
- try {
- br = new BufferedReader(new InputStreamReader(
- new FileInputStream(f), "utf-8"));
- filewriter = new FileWriter(file, false);
- SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.println(sd.format(new Date()));
- String tempString = null;
- int i = 0;
- while ((tempString = br.readLine()) != null) {
- if (i < starRow-1) {
- i++;
- continue;
- }
- if(tempString.trim().equals(""))
- break;
- if (StringUtil.contains(tempString, "/"")) {
- tempString = deepParser(tempString,filePath);
- } else
- tempString = StringUtil.replace(tempString, ",", "|");
- // System.out.println(tempString);
- filewriter.write(stringTrim(tempString, "//|") + "/r/n");
- i++;
- }
- System.out.println(sd.format(new Date()));
- } catch (Throwable e) {
- log.warn("解析文件:【" + filePath + "】出错", e);
- e.printStackTrace();
- } finally {
- try {
- br.close();
- filewriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static String deepParser(String str,String filePath) {
- System.out.println(str);
- String temp = str;
- str = str+",";
- StringBuffer sb = new StringBuffer();
- try {
- int from = 0;
- int end = str.length();
- int i = 0;
- while (StringUtil.contains((str = str.substring(from)), "/"")) {
- from = str.indexOf("/"");
- end = str.indexOf("/"", from + 1);
- sb.append(StringUtil.replace(str.substring(0, from), ",", "|"));
- sb.append(str.substring(from + 1, end));
- from = end + 1;
- i++;
- }
- sb.append(StringUtil.replace(str, ",", "|"));
- } catch (Throwable e) {
- log.warn("解析文件:【" + filePath + "】出错,一下数据有问题:"+temp, e);
- e.printStackTrace();
- }
- String s = sb.toString();
- s = s.substring(0, s.lastIndexOf("|"));
- return s;
- }
- //去除字段2边空格,可以指定分隔符
- public static String stringTrim(String str, String regex) {
- str = str+" ";
- String[] strs = str.split(regex);
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < strs.length; i++) {
- sb.append(strs[i].trim() + "|");
- }
- return sb.toString().substring(0, sb.toString().lastIndexOf("|"));
- }
CSV 转TXT
最新推荐文章于 2024-06-17 09:10:54 发布