将2D数组写入CSV文件 [英] Writing a 2d array to a csv file
本文介绍了将2D数组写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有2d数组的程序,我需要写入一个csv文件,但是我只需要写该数组的一个索引.我正在使用.get(0).set(4,newAmount)在第一个索引中设置一个值,但如果有必要,我需要将索引的所有值写入文件.
I have a program that has a 2d array and I need to write to a csv file but I only need to write one one index of the array. I'm using the .get(0).set(4, newAmount) to set a value in the first index but I need to write all values of the index to a file if that makes sense.
fw = new FileWriter(FILE_NAME, true);
bw = new BufferedWriter(fw);
System.out.println("Enter the name of the person you want to change amount for");
String changeName = FileUtility.getInput().nextLine();
String newAmount;
Scanner s = new Scanner(new File(FILE_NAME));
String str = "";
List<List<String>> list = new ArrayList<List<String>>();
while (s.hasNext()) {
list.add(Arrays.asList(s.nextLine().split(",")));
}
if (list.get(0).get(0).equalsIgnoreCase(changeName)) {
System.out.println("Enter the new amount paid for the player");
newAmount = FileUtility.getInput().nextLine();
list.get(0).set(4, newAmount);
//write to csv file here
推荐答案
将此代码放在代码底部.
put this code in bottom of your code.
String csv = list.stream()
.map(row -> row.stream()
.map(col -> col.toString())
.collect(Collectors.joining(", ")))
.collect(Collectors.joining("\n"));
writer.write(csv);
这篇关于将2D数组写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文