[英]Writing a 2d array to a csv file
我有一個具有2d數組的程序,我需要寫入一個csv文件,但是我只需要寫該數組的一個索引。 我正在使用.get(0).set(4,newAmount)在第一個索引中設置一個值,但是如果有必要,我需要將該索引的所有值寫入文件。
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
將此代碼放在代碼底部。
String csv = list.stream()
.map(row -> row.stream()
.map(col -> col.toString())
.collect(Collectors.joining(", ")))
.collect(Collectors.joining("\n"));
writer.write(csv);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.