CSV 转TXT

  1. public static void specialChar(String filePath,int starRow) throws Exception {   
  2.   
  3.         BufferedReader br = null;   
  4.         File f = new File(filePath);   
  5.         String fileName = f.getName();   
  6.   
  7.         if (!fileName.substring(fileName.indexOf(".") + 1).equals("csv")) {   
  8.             throw new Exception(filePath + "不是一个CSV文件");   
  9.         }   
  10.         File file = new File(StringUtil.replace(f.getPath(), "csv""txt"));   
  11.         FileWriter filewriter = null;   
  12.         try {   
  13.             br = new BufferedReader(new InputStreamReader(   
  14.                     new FileInputStream(f), "utf-8"));   
  15.             filewriter = new FileWriter(file, false);   
  16.             SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
  17.   
  18.             System.out.println(sd.format(new Date()));   
  19.             String tempString = null;   
  20.             int i = 0;   
  21.             while ((tempString = br.readLine()) != null) {   
  22.                 if (i < starRow-1) {   
  23.                     i++;   
  24.                     continue;   
  25.                 }   
  26.                 if(tempString.trim().equals(""))   
  27.                     break;   
  28.                 if (StringUtil.contains(tempString, "/"")) {   
  29.                     tempString = deepParser(tempString,filePath);   
  30.                 } else  
  31.                     tempString = StringUtil.replace(tempString, ",""|");   
  32. //              System.out.println(tempString);   
  33.                 filewriter.write(stringTrim(tempString, "//|") + "/r/n");   
  34.                 i++;   
  35.             }   
  36.             System.out.println(sd.format(new Date()));   
  37.         } catch (Throwable e) {   
  38.             log.warn("解析文件:【" + filePath + "】出错", e);   
  39.             e.printStackTrace();   
  40.         } finally {   
  41.             try {   
  42.                 br.close();   
  43.                 filewriter.close();   
  44.             } catch (IOException e) {   
  45.                 e.printStackTrace();   
  46.             }   
  47.         }   
  48.   
  49.     }   
  50.   
  51.     public static String deepParser(String str,String filePath) {   
  52.         System.out.println(str);   
  53.         String temp = str;   
  54.                           str = str+",";   
  55.         StringBuffer sb = new StringBuffer();   
  56.         try {   
  57.             int from = 0;   
  58.             int end = str.length();   
  59.             int i = 0;   
  60.             while (StringUtil.contains((str = str.substring(from)), "/"")) {   
  61.                 from = str.indexOf("/"");   
  62.                 end = str.indexOf("/"", from + 1);   
  63.                 sb.append(StringUtil.replace(str.substring(0, from), ",""|"));   
  64.                 sb.append(str.substring(from + 1, end));   
  65.                 from = end + 1;   
  66.                 i++;   
  67.             }   
  68.             sb.append(StringUtil.replace(str, ",""|"));   
  69.         } catch (Throwable e) {   
  70.             log.warn("解析文件:【" + filePath + "】出错,一下数据有问题:"+temp, e);   
  71.             e.printStackTrace();   
  72.         }   
  73.                        String s = sb.toString();   
  74.              s = s.substring(0, s.lastIndexOf("|"));   
  75.              return s;   
  76.     }   
  77.   
  78.   
  79.     //去除字段2边空格,可以指定分隔符   
  80.     public static String stringTrim(String str, String regex) {   
  81.         str = str+" ";   
  82.         String[] strs = str.split(regex);   
  83.         StringBuffer sb = new StringBuffer();   
  84.   
  85.         for (int i = 0; i < strs.length; i++) {   
  86.             sb.append(strs[i].trim() + "|");   
  87.         }   
  88.   
  89.         return sb.toString().substring(0, sb.toString().lastIndexOf("|"));   
  90.     }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值