zoukankan      html  css  js  c++  java
  • 读取txt文件,生成csv文件

    最近做了个小程序,要求在同文件夹下的txt文件,处理内容之后,生成csv文件。

      1 import java.io.*;
      2 import java.util.ArrayList;
      3 import java.util.List;
      4 
      5 public class Simplify {
      6 
      7     public static void main(String[] args) {
      8 
      9         List<String> resultList = readTxt();
     10 
     11         outCsv(resultList);
     12 
     13         System.out.println(System.getProperty("user.dir"));
     14     }
     15 
     16     /* 处理文件内容 */
     17     private static List<String> readTxt() {
     18 
     19 //        String fileName = Simplify.class.getClassLoader().getResource("").getPath() + "\jacoco.txt";
     20         String fileName = System.getProperty("user.dir") + "\jacoco.txt";
     21         File file = new File(fileName);
     22         BufferedReader reader = null;
     23         List<String> resultList = new ArrayList<>();
     24         String[] tempStrs;
     25         String[] packageClassStrs;
     26         String packageClass = "";
     27 
     28         try {
     29             reader = new BufferedReader(new FileReader(file));
     30             String tempString;
     31             // 一次读入一行,直到读入null为文件结束
     32             while ((tempString = reader.readLine()) != null) {
     33                 if (tempString.contains(" class ")) {
     34                     tempString = tempString.substring(tempString.indexOf("class")).replaceAll("/", ".");
     35                     tempStrs = tempString.split(" ");
     36                     packageClass = tempStrs[2];
     37                 } else if (tempString.contains(" method ") && !tempString.contains("<init>()V")) {
     38                     tempString = tempString.substring(tempString.indexOf("method")).replaceAll("/", ".");
     39                     tempStrs = tempString.split(" ");
     40                     packageClassStrs = packageClass.split("\.");
     41                     String cla = packageClassStrs[packageClassStrs.length - 1];
     42                     String pac = packageClass.substring(0, packageClass.length() - cla.length() - 1);
     43                     resultList.add(pac + "," + cla + "," + tempStrs[1].substring(0, tempStrs[1].indexOf("(")));
     44                 }
     45             }
     46 
     47         } catch (IOException e) {
     48             e.printStackTrace();
     49         } finally {
     50             if (reader != null) {
     51                 try {
     52                     reader.close();
     53                 } catch (IOException e1) {
     54                     e1.printStackTrace();
     55                 }
     56             }
     57         }
     58         return resultList;
     59     }
     60 
     61     /* 输出csv文件 */
     62     private static void outCsv(List<String> resultList) {
     63 
     64         FileOutputStream out = null;
     65         OutputStreamWriter osw = null;
     66         BufferedWriter bfw = null;
     67 
     68         String fileName = System.getProperty("user.dir") + "\jacoco.csv";
     69 //        String fileName = Simplify.class.getClassLoader().getResource("").getPath() + "\jacoco.csv";
     70         try {
     71             out = new FileOutputStream(fileName);
     72             osw = new OutputStreamWriter(out, "UTF8");
     73             bfw = new BufferedWriter(osw, 1024);
     74 
     75             if (resultList != null && !resultList.isEmpty()) {
     76                 for (String result : resultList) {
     77                     bfw.write(result + "
    ");
     78                 }
     79             }
     80             bfw.flush();
     81 
     82         } catch (Exception e) {
     83             e.printStackTrace();
     84         } finally {
     85             try {
     86                 if (out != null) {
     87                     out.close();
     88                 }
     89                 if (osw != null) {
     90                     osw.close();
     91                 }
     92                 if (bfw != null) {
     93                     bfw.close();
     94                 }
     95             } catch (IOException e1) {
     96                 e1.printStackTrace();
     97             }
     98         }
     99     }
    100 }
    System.getProperty("user.dir")相对路径的取法

  • 相关阅读:
    变量可变性问题
    Android 创建Listener监听器形式选择:匿名内部类?外部类?
    linux下安装zookeeper
    翻页工具类
    将哈夫曼树转化成二叉树
    Activity的启动流程分析
    题目1186:打印日期
    数据库设计--数据流图(DFD)
    c#基础之数组
    10.3.1 一个CONNECT BY的样例
  • 原文地址:https://www.cnblogs.com/CuiHongYu/p/9987429.html
Copyright © 2011-2022 走看看