简体   繁体   English

读取CSV文件并用Java编写Json文件

[英]Read a CSV file and Write a Json file in Java

I modified my code: 我修改了代码:

private static final String SAMPLE_CSV_FILE_PATH = "src/main/resources/testCSV.csv";
private static final String OUT_PUT_CSV_PATH = "src/main/resources/outCSV.csv";

public static void main(String[] args) throws IOException {

    Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
    CSVReader csvReader = new CSVReader(reader);



    List<String[]> records = csvReader.readAll();
    Writer writer = Files.newBufferedWriter(Paths.get(OUT_PUT_CSV_PATH));
    CSVWriter out = new CSVWriter(writer);

    int i = 1;
    int total = 0;
    while(i < records.size()){
        String[] result = records.get(i);
        for(int j =1; j<= Integer.parseInt(result[1]); j++){
            String pattern="00000";
            DecimalFormat myFormatter = new DecimalFormat(pattern);
            String output = myFormatter.format(j);
            writer.append(result[0]+output+"\n");
            total++;
        }
        i++;
    }
    out.flush();
    out.close();
    System.out.println(total);
}

第一个CSV

Now I am using the first CSV file to generate the serial number, Something like: 现在,我使用第一个CSV文件生成序列号,如下所示:

NAIS00001
NAIS00002
...
NAIS00625

Then I write these serial numbers into a new CSV file. 然后,我将这些序列号写入新的CSV文件。 But there is only one column. 但是只有一列。 6 millions data in one column... How can I star a new column? 一列中有600万个数据...如何给新列加注星标?

Your Filewriter is not writing in append mode, so your file is being overwritten each time it goes through the outer loop. 您的Filewriter不是以追加模式编写的,因此,每次通过外部循环时,文件都会被覆盖。 It's not a problem with the file size. 文件大小没有问题。

Try this: 尝试这个:

FileWriter fileWriter = new FileWriter("src/main/resources/testOutPut.json", true);

Documentation 文献资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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