Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何避免/删除在一行中写入最后一个逗号?_Java_Swing_Csv_Jtable - Fatal编程技术网

Java 如何避免/删除在一行中写入最后一个逗号?

Java 如何避免/删除在一行中写入最后一个逗号?,java,swing,csv,jtable,Java,Swing,Csv,Jtable,我使用的是JTable和CSV文件,问题是我必须保存插入表中的数据,然后在用户需要进行一些修改时重新打开。到目前为止,我可以保存并加载从我刚才提到的表创建的CSV文件,但是BufferedWriter对象在每行末尾添加了一个逗号,这会在加载现有CSV文件时导致一个新的空白列 这是我的代码,我试图限制这件事,但没有成功 if(jTable1.getModel()==this.m){ File file = new File(this.tfSelectedSpreadshee

我使用的是
JTable
和CSV文件,问题是我必须保存插入表中的数据,然后在用户需要进行一些修改时重新打开。到目前为止,我可以保存并加载从我刚才提到的表创建的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();
        }

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

我总是在项目之前添加逗号,除非索引为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();
BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream)(this.tfSelectedSpreadsheet.getText().toString(),“UTF-8”);
对于(int i=0;i 0){
bfw.追加(“,”);
}
追加(jTable1.getColumnName(i));
}
bfw.newLine();

由于Java 8,您可以使用:

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

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

String row = joiner.toString(); // produces "[First,Second]"
如果您的数据已经可以作为集合(或其他可以产生循环的内容)使用,您也可以使用新的流API,而不是编写另一个循环:

List<String> data = ...
String row = data.stream().collect(Collectors.joining(","));
列表数据=。。。
String row=data.stream().collect(collector.joining(“,”);

…相反

多亏了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();
        }
if(jTable1.getModel()==this.m){
File File=新文件(此.tfSelectedSpreadsheet.getText().toString());
if(file.exists()&&!file.isDirectory()){
delete();
}
BufferedWriter bfw=新的BufferedWriter(新的OutputStreamWriter(新的文件OutputStream(this.tfSelectedSpreadsheet.getText().toString()),“UTF-8”);

对于(inti=0;这是一个常见的问题,据我所知,没有“聪明”的解决方案。您必须在循环中的某个位置插入测试(或将循环展开一次迭代).教我一些新东西+1我现在不太会用Java,但这是一个方便的Java 8知识。谢谢!@shmosel根据我偷来的示例代码,它是
I
,它在写列标题,但是是的,它适用于OP测试的所有地方
getColumnCount
我的错,我在看第二个非常感谢@MadProgrammer我听从了你的建议,把条件改成了IF子句。
List<String> data = ...
String row = data.stream().collect(Collectors.joining(","));
if(i<jTable1.getColumnCount()) bfw.append(",");
if(i< == Table1.getColumnCount() - 1) bfw.append(",");
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();
        }