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

时间:2018-05-01 20:57:36

标签: java json filewriter json-simple

我修改了我的代码:

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万个数据...如何为新列添加星标?

1 个答案:

答案 0 :(得分:3)

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

试试这个:

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

Documentation