简体   繁体   中英

Read, parse and then write a CSV in Java

I am trying to read in a csv file, parse it and the write a new csv with the parsed data.

My input .csv file looks like this:

[Altice<SPLIT> France<SPLIT>
nNUMFP<SPLIT> 4.875<SPLIT> 19<SPLIT> 99.875<SPLIT>-<SPLIT>100.375<SPLIT> 4.909<SPLIT>/<SPLIT>4.752<SPLIT> 371.<SPLIT>/<SPLIT>371.<SPLIT> 2.4MMM<SPLIT> B+<SPLIT> Ba3<SPLIT>
nNUMFP<SPLIT> 6<SPLIT> 22<SPLIT> 102.000<SPLIT>-<SPLIT>102.500<SPLIT> 5.559<SPLIT>/<SPLIT>5.450<SPLIT> 422.<SPLIT>/<SPLIT>411.<SPLIT> 4MMM<SPLIT> B+<SPLIT> Ba3<SPLIT>
nNUMFP<SPLIT> 6.25<SPLIT> 24<SPLIT> 103.000<SPLIT>-<SPLIT>103.750<SPLIT> 5.741<SPLIT>/<SPLIT>5.616<SPLIT> 420.<SPLIT>/<SPLIT>407.<SPLIT> 1.375M<SPLIT> B+<SPLIT> Ba3<SPLIT>
nAltice<SPLIT> S.A.<SPLIT>
nATCNA<SPLIT> 7.75<SPLIT> 22<SPLIT> 103.250<SPLIT>-<SPLIT>104.000<SPLIT> 7.005<SPLIT>/<SPLIT>6.837<SPLIT> 568.<SPLIT>/551.<SPLIT> 2.9MMM<SPLIT> B<SPLIT> B3<SPLIT>
nATCNA<SPLIT> 7.625<SPLIT> 25<SPLIT> 101.875<SPLIT>-<SPLIT>102.375<SPLIT> 7.309<SPLIT>/<SPLIT>7.227<SPLIT> 573.<SPLIT>/<SPLIT>565.<SPLIT> 1.48MM<SPLIT> N.A.<SPLIT> B3e<SPLIT>
n

My code is:

public class TokenizerParser {

    public static void main(String[] args) throws Exception {
          //Build reader instance
          CSVReader reader = new CSVReader(new FileReader("rawtest.csv"), ',', '"', 1);
          CSVWriter writer = new CSVWriter(new FileWriter(csv));

          //Read all rows at once
          List<String[]> allRows = reader.readAll();

          //Read CSV line by line and use the string array as you want
         for(String[] row : allRows){
             String changedString = Arrays.toString(row).replace("[", ""); // This is an example for parsing
             System.out.println(changedString);

         }

         // Here I do not know what to do to write the parsed data back to a .csv
         for(String[] output : allRows) {

         }
    }
}

So I can read and do the parsing operations, but how can write the parsed Strings which I can see from the System.out.println(changedString) in the console to a new .csv file?

Thank you very much for any help!

You are saving the parsed/changed String locally in your for-loop. If you save this String to another List, you can reuse it later.

List<String> parsedStrings = new ArrayList<>();
for(String[] row : allRows){
    String changedString = Arrays.toString(row).replace("[","");
    parsedsString.add(changedString);
}

Try this:

    for(String[] output : allRows) {

        //get current row
        String[] parsedRow=new String[output.length];
        for(int i=0;i<output.length;i++){
            //parse each column
            parsedRow[i]=output[i].replace("[", "");
        }

        //write line
        writer.writeNext(parsedRow);


    }
    writer.close();

尝试Super CSV ,它功能强大而强大。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM