如何在指定列的CSV文件中写入值
假设我想写第4列,假设第一行的前三列已经有值。
我无法做到
下面是我的代码
1064 you have an error in your sql syntax near 'MAX(IF(purchasables.sku = '999992', lineitems.qty, NULL)) AS '999992',MAX(IF(pur' at line 1
答案 0 :(得分:0)
这是一个简单的CSV编辑类,基于1的索引。
public final class CSV {
private final List<String[]> cells;
public CSV(File file) throws IOException {
cells = new ArrayList<>();
try (Scanner scan = new Scanner(file)) {
while (scan.hasNextLine()) {
String line = scan.nextLine();
cells.add(line.split(","));
}
}
}
public String get(int row, int col) {
String columns = cells.get(row - 1);
return columns[col - 1];
}
public CSV set(int row, int col, String value) {
String columns = cells.get(row - 1);
columns[col - 1] = value;
return this;
}
public void save(File file) throws IOException {
try (PrintWriter out = new PrintWriter(file)) {
for (String[] row : cells) {
for (String cell : row) {
if (cell != row[0]) {
out.print(",");
}
out.print(cell);
}
out.printLn();
}
}
}
}
现在你可以做到
File file = new
File("C:\\docs\\test\\COLD_SOAK_1_engine_20171002014945.csv");
new CSV(file).set(1, 4, "new value").save(file);
但这对于在值中引用逗号的CSV文件不起作用。