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

标签 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 万数据...如何为新栏加注星标?

最佳答案

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

试试这个:

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

Documentation

关于java - 读取 CSV 文件并用 Java 编写 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123732/

相关文章:

java - 从递归方法返回数组

java - 对Integer对象进行加操作,从目录中读取多个文件以在Java中创建词袋

json - 导轨 API : make application controller serve JSON

json - Angularjs ng-repeat 在 JSON 的多层上进行重复过滤

java - 在java中创建一个文本文件

java - Spring Batch 将并发线程数限制为 10

java - 如何在单行中绘制具有不同颜色的折线图

jquery - 在 json 中循环 - jquery

java - 如何在 Java 中更新现有文件?

java - 在java中连续写入一行文本