如何避免/删除连续写入最后一个逗号?

时间:2015-02-17 23:44:30

标签: java swing csv jtable

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

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

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,

为了解决这个“逗号”问题,我该怎么做?

4 个答案:

答案 0 :(得分:9)

从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(","));

答案 1 :(得分:4)

除非索引为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();

答案 2 :(得分:3)

if(i<jTable1.getColumnCount()) bfw.append(",");

i将始终小于jTable1.getColumnCount(),请记住,Java已被0编入索引,因此列数来自0-jTable1.getColumnCount() - 1

尝试使用更像......

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

...代替

答案 3 :(得分:0)

感谢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();
        }