我修改了我的代码:
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 文件生成序列号,类似于:
NAIS00001
NAIS00002
...
NAIS00625
然后我将这些序列号写入一个新的 CSV 文件中。但是只有一列。一栏 600 万数据...如何为新栏加注星标?
最佳答案
您的 Filewriter 没有以追加模式写入,因此每次通过外循环时您的文件都会被覆盖。这不是文件大小的问题。
试试这个:
FileWriter fileWriter = new FileWriter("src/main/resources/testOutPut.json", true);
关于java - 读取 CSV 文件并用 Java 编写 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123732/