如何 avoid/remove 连续写最后一个逗号?

How to avoid/remove writing the last comma in a row?

我正在处理 JTable 和 CSV 文件,问题是我必须保存插入 table 中的数据,然后在用户需要进行一些修改时重新打开。到目前为止,我可以保存和加载从我正在谈论的 table 创建的 CSV 文件,但是 BufferedWriter 对象在每一行的末尾添加一个逗号,这会在加载现有的时导致一个新的空白列CSV 文件。

这是我的代码,我试图限制这件事,但没有成功。

if(jTable1.getModel()==this.m){
            File file = new File(this.tfSelectedSpreadsheet.getText().toString());
            if(file.exists() && !file.isDirectory()){
                file.delete();
            }
            BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.tfSelectedSpreadsheet.getText().toString()),"UTF-8"));
            for(int i=0; i<jTable1.getColumnCount();i++){
                bfw.append(jTable1.getColumnName(i));
                if(i<jTable1.getColumnCount()) bfw.append(",");
                else bfw.newLine();
            }
            bfw.newLine();
            for(int i=0;i<jTable1.getRowCount();i++){
                for(int j=0;j<jTable1.getColumnCount();j++){
                    bfw.append((String)jTable1.getValueAt(i,j));
                    if(j<jTable1.getColumnCount()) bfw.append(",");
                    bfw.newLine();
                }
                bfw.newLine();
            }
            bfw.close();
        }

这就是这段代码生成的 CSV 文件的样子:

Link,Long,Lat,CRS,
R011,120.1126,-33.5422,GDA94,
R014,120.1126,-33.5422,GDA94,
R019,120.1126,-33.5422,GDA94,
R011,120.1126,-33.5422,GDA94,
R011,120.1126,-33.5422,GDA94,
R011,120.1126,-33.5422,GDA94,
R015,120.1126,-33.5422,GDA94,
R014,120.1126,-33.5422,GDA94,
WANDA,25.12.1990,-37.0991,GDA90,

我该怎么做才能解决这个 "comma" 问题?

除非索引为 0,否则我总是在项目前添加逗号。

BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.tfSelectedSpreadsheet.getText().toString()),"UTF-8"));
for(int i=0; i<jTable1.getColumnCount();i++){
    if (i > 0) {
        bfw.append(",");
    }
    bfw.append(jTable1.getColumnName(i));
}
bfw.newLine();

因为 Java 8 你可以使用 StringJoiner:

StringJoiner joiner = new StringJoiner(",");
joiner.add("First");
joiner.add("Second");

String row = joiner.toString(); // produces "First,Second"

您甚至可以定义前缀和后缀:

StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("First");
joiner.add("Second");

String row = joiner.toString(); // produces "[First,Second]"

如果您的数据已经作为集合可用(或其他可以产生 Stream 的数据),您还可以使用新流 API 而不是编写另一个循环:

List<String> data = ...
String row = data.stream().collect(Collectors.joining(","));
if(i<jTable1.getColumnCount()) bfw.append(",");

i 将始终小于 jTable1.getColumnCount(),请记住,Java0 索引的,因此列数从 0-jTable1.getColumnCount() - 1

尝试使用更像...

if(i< == Table1.getColumnCount() - 1) bfw.append(",");

...改为

感谢 MadProgrammer 我使用这段代码解决了这个问题,我希望它能帮助那里的人。

if(jTable1.getModel()==this.m){
            File file = new File(this.tfSelectedSpreadsheet.getText().toString());
            if(file.exists() && !file.isDirectory()){
                file.delete();
            }
            BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.tfSelectedSpreadsheet.getText().toString()),"UTF-8"));
            for(int i=0; i<jTable1.getColumnCount();i++){
                bfw.append(jTable1.getColumnName(i));
                if(i<jTable1.getColumnCount()-1) bfw.append(",");//Here i changed the condition
                else bfw.newLine();
            }
            for(int i=0;i<jTable1.getRowCount();i++){
                for(int j=0;j<jTable1.getColumnCount();j++){
                    bfw.append((String)jTable1.getValueAt(i,j));
                    if(j<jTable1.getColumnCount()-1) bfw.append(",");//Here i changed the condition
                    else bfw.newLine();
                }
            }
            bfw.close();
        }