2016-03-22 9 views
2

不規則なcsvファイルを印刷して変更できるプログラムを作成したかったのです。次のようなフォーマットがある:私は初心者ですJavaで複雑なcsvファイルを変更する

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

、次のようにデータが与えられることができます問題。私の主な関心事は、ループを繰り返してクラブの日付と名前を特定し、それを配列にどのように供給するのですか? お知らせください。

+0

ですその第2のコード領域は、例示的なスキーマまたはサンプルファイルですか? CSVファイルのようには見えません。 –

+1

実際にはそれほど難しいことではありません。スキャナを使って1行ずつ読み込みます。 SimpleDateFormatterを使用してデータを保持すべきものを解析することができます。次の行...あなたは単に文字列を保存し、次の行は、CSVパーサにフィードします(その用語のためのGoogleだけで、それを助けるライブラリがたくさんあります)。 – GhostCat

答えて

1

私はこれがあなたに役立つはずだと思います。基本的にあなたの混乱した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(); 
    } 

ですこれは、私がcsv.txtからファイルを読んでいます。この

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), 

のために取得します出力されます。ループはテキストファイルの各行を通ります。すべてのフィールドは変数に格納されます。次の日が来ると、それらをすべて出力ファイルに書き出します。 whileループが終了した後、csvの最後の行がファイルに書き込まれます。

+1

ありがとうございました。あなたのコードは非常に明確で理解しやすいです。これは私を助けました。 –

+0

あなたは大歓迎です。 – denis

1

これを処理するにはuniVocity-parsersを試してください。このような形式の解析には、hereの例があります。執筆のために、herehereを見てください。

私が与えてくれた例から適応、あなたが書くことができる:

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のライセンス)です

関連する問題