Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中修改复杂的csv文件_Java_Csv - Fatal编程技术网

在java中修改复杂的csv文件

在java中修改复杂的csv文件,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 n

我想写一个程序,可以打印,修改不规则的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
例如,数据可以如下所示:

    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循环遍历文本文件的每一行。所有字段都存储在一个变量中。当下一个日期到来时,我将它们全部写入输出文件。csv的最后一行在while循环终止后写入文件。

尝试处理此问题。对于解析这种格式,您将发现一些示例。对于书写,请查看和查看

根据我给出的例子,你可以写:

final ObjectRowListProcessor dateProcessor = new ObjectRowListProcessor();
final ObjectRowListProcessor clubProcessor = new ObjectRowListProcessor();
final ObjectRowListProcessor memberProcessor = new ObjectRowListProcessor();

InputValueSwitch switch = new InputValueSwitch(0){
    public void rowProcessorSwitched(RowProcessor from, RowProcessor to) {
    //your custom logic here
    if (to == dateProcessor) {
        //processing dates.
    }
    if (to == clubProcessor) {
        //processing clubs.
    }
    if (to == memberProcessor){
        //processing members
    }
};

switch.addSwitchForValue("1.", dateProcessor, 1);  //getting values of column 1 and sending them to `dateProcessor`
switch.addSwitchForValue("2.", clubProcessor, 1); //getting values of column 1 and sending them to `clubProcessor`
switch.addSwitchForValue("3.", memberProcessor, 1, 2, 3); //getting values of columns 1, 2, and 3 and sending them to `memberProcessor` 
setDefaultSwitch(memberProcessor, 1, 2, 3); //Rows with blank value at column 0 are members. Also get columns 1, 2, and 3 and send them to `memberProcessor`

CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial and examples

// configure the parser to use the switch
settings.setRowProcessor(switch);

//creates a parser
CsvParser parser = new CsvParser(settings);

//parse everying. Rows will be sent to the RowProcessor of each switch, depending on the value at column 0.
parser.parse(new File("/path/to/file.csv")); 

免责声明:我是这个库的作者,它是开源和免费的(Apache 2.0许可证)

第二个代码区域是示例模式还是示例文件?它看起来不像一个CSV文件,实际上这并不太难:你用扫描仪逐行读取;应该携带数据的数据可以使用SimpleDataFormatter进行解析;下一行。。。您只需存储字符串,下一行输入CSV解析器(只需谷歌搜索该术语;您会得到很多库来帮助您实现这一点)。非常感谢您的输入。您的代码非常清晰易懂。这对我帮助很大。