在Java中修改复杂的CSV文件 [英] Modifying complex csv files in java

查看:572
本文介绍了在Java中修改复杂的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to write a program which can print, and modify the irregular csv files. The format is as follows:

    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
      ..........

例如,数据可以如下所示:

For instance, the data may be given as follows:

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

我是一个初学者,但是我没有在线上找到任何可行的资源来解决此类问题.我主要关心的是,我们如何遍历循环并确定俱乐部的日期和名称,并将其输入数组? 请告知.

I am a beginner and I have not found any viable resource online which deals with this type of problem. My main concern is, how do we iterate through the loop and identify the date and name of the club, and feed them into a array? Please advise.

推荐答案

我认为这对您有帮助.基本上,在您弄乱的csv中应该有一些模式.下面是我安排您的csv的代码

I think this should be helpful for you. Basically there should be some pattern in your messed up csv. Below is my code to arrange your 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();
  }

这是您将为此获得的输出

This is the output you will get for this

   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的最后一行写入文件.

I am reading the file from csv.txt. while loop goes through each line of text file.all the fields are stored in a variable. When next date comes I write all of them into output file. Last line of the csv is written to file after the while loop terminates.

这篇关于在Java中修改复杂的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆