java - 在java中修改复杂的csv文件

标签 java csv

我想编写一个可以打印和修改不规则csv文件的程序。格式如下:

    1.date
    2.organization name
    3. student name, id number, residence
       student name, id number, residence
       student name, id number, residence
       student name, id number, residence
       student name, id number, residence
    1.another date
    2.another organization name
    3. student name, id number, residence
       student name, id number, residence
       student name, id number, residence
      ..........

例如,数据可以如下给出:

1. 10/09/2016
2. cycling club
3. sam, 1000, oklahoma
   henry, 1001, california
   bill, 1002, NY
1. 11/15/2016
2. swimming club
3. jane, 9001, georgia
   elizabeth, 9002, lousiana

我是初学者,我没有在网上找到任何处理此类问题的可行资源。我主要关心的是,我们如何迭代循环并识别俱乐部的日期和名称,并将它们输入数组中? 请指教。

最佳答案

我想这应该对你有帮助。基本上,你困惑的 csv 中应该有一些模式。下面是我用来排列 csv 的代码

   public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

        PrintWriter writer = new PrintWriter("file.txt", "UTF-8");
            try{

              //Create object of FileReader
              FileReader inputFile = new FileReader("csv.txt");

              //Instantiate the BufferedReader Class
              BufferedReader bufferReader = new BufferedReader(inputFile);

              //Variable to hold the one line data
              String line;
              String date="";String org ="";String student ="";
              // Read file line by line and print on the console
              while ((line = bufferReader.readLine()) != null)   {
                  if(line.contains("1.")){
                      if(date!="" || org!=""){
                          writer.println(date+","+org+","+student);
                          student ="";
                       }
                         date = line.substring(2);
                        }else if(line.contains("2.")){
                            org = line.substring(2);

                      }else{
                            line = "("+line+")";
                          student += line+",";
                  }

                    System.out.println(line);
              }
              writer.println(date+","+org+","+student);
              //Close the buffer reader
              bufferReader.close();
      }catch(Exception e){
        System.out.println("Error while reading file line by line:" + e.getMessage());                      
   }
       writer.close();
  }

这是您将获得的输出

   10/09/2016, cycling club,(3. sam, 1000, oklahoma),(   henry, 1001, california),(   bill, 1002, NY),
   11/15/2016, swimming club,(3. jane, 9001, georgia),(   elizabeth, 9002, lousiana),

我正在从 csv.txt 读取文件。 while 循环遍历文本文件的每一行。所有字段都存储在变量中。当下一个日期到来时,我将它们全部写入输出文件。 while 循环终止后,csv 的最后一行将写入文件。

关于java - 在java中修改复杂的csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36143952/

相关文章:

python - 清理 python pandas 中的日期和时间记录

Java:无法从套接字读取,线程卡在 readLine() 上

Java如何在现有接口(interface)之上构建流

Java 使用 HashMap 或 Map 与多维数组

java.sql.SQLException : Connection com. mysql.jdbc.JDBC4Connection@1ab9c42 已关闭

c# - "\",\x0A\x0D"代码在编写 CSV 时在 C# 中的作用

c# - 在没有标题的情况下将 CSV 文件读入数据集

python - 如何将csv数据导入django模型

Linux:将字数 append 到文件的每一行

Java EE 网络安全