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

问题描述 投票:1回答:1

我修改了我的代码:

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);
}

First CSV

现在我使用第一个CSV文件生成序列号,类似于:

NAIS00001
NAIS00002
...
NAIS00625

然后我将这些序列号写入新的CSV文件。但是只有一个专栏。一列中有600万个数据...如何为新列加注星标?

java json filewriter json-simple
1个回答
3
投票

您的Filewriter没有以附加模式写入,因此每次通过外部循环时都会覆盖您的文件。这不是文件大小的问题。

试试这个:

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

Documentation

© www.soinside.com 2019 - 2024. All rights reserved.