首页 > 解决方案 > 如何在将值附加到 Java 中的 CSV 文件时为每次迭代仅附加一行/行

问题描述

我在将数据附加到我的 CSV 文件方面遇到了一些问题。问题是,每当我第二次尝试将数据附加到我的 CSV 文件中时,附加到 CSV 文件的第二个值与第一个附加值一起出现。就像它在附加到 CSV 文件时带来了现有值。因此,由于这个问题,它会导致此语句中的数组索引越界异常: cust[read2DStringIndex][newVarIndexer] = fromfile[g]; ,CSV 文件的数据重复现有值以及最新的附加值,并且第一个值仅显示在我的 GUI 表上。

CSV 文件: CSV 文件

桌子:

桌子

这是我编写和读取 CSV 的源代码:

public void writeCustomerCSV(){  // this creates a CSV file which stores the inputs of the user
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv",true)); // when I set append mode to true,  cust[read2DStringIndex][newVarIndexer] = fromfile[g] results to index array out of bounds to 10
        StringBuilder sb = new StringBuilder();
        int y;
        for(int x = 0; x < itemTo2D.length; x++){
            if(itemTo2D[x][0] != null){
                for(y = 0; y < itemTo2D[0].length; y++){
                    sb.append(itemTo2D[x][y]);
                    sb.append(",");
                }
            }
            sb.append("-");  //separation for rows
            sb.append(",");  // separation for columns
        }
        bw.write(sb.toString());
        bw.close();
    }
    catch (Exception ex){
    }
}

public void readCustomerCSV(){  // reads the contents of the CSV file 
    String[][] twoDArray = new String[10][7];
    int read2DStringIndex = 0;
    int newVarIndexer = 0;
    DefaultTableModel tblmodelll = (DefaultTableModel) mainTable.getModel();  // table
    String[] fromfile = {};   // 1d string for getting the columns(7 columns) of the CSV file
    int ak = 0;
    int sk = 0;
    try{
        BufferedReader br = new  BufferedReader(new FileReader("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
        String line;
        while ((line = br.readLine()) != null){
            fromfile = line.split(",");  //separates the columns by a comma
            for(int c = 0; c < fromfile.length; c++){
                if(fromfile[c].equals("-")){
                    sk = 0;
                    ak++;
                    if(c > 0){
                        if(!fromfile[c-1].equals("-")){
                            id = id + 1;
                        }
                    }
                } else{
                    twoDArray[ak][sk] = fromfile[c];
                    sk++;
                }
            }
        }
    } catch (Exception ex){

    }
    for(int g = 0; g < fromfile.length; g++){  
        if(fromfile[g].equals("-")){   //if there is a presence of a dash, it increments the read2DStringINdex (row index) of the 2D array
            read2DStringIndex++;
            newVarIndexer = 0;
        }
        else{
            cust[read2DStringIndex][newVarIndexer] = fromfile[g];    //cust is the 2D array(declared universal) which is going to display the values to the table
            newVarIndexer++;
        }
    }
    for(int h = 0; h < cust.length; h++){  //prints cust (2D array) , just to check what data is being stored
        for(int p = 0; p < cust[0].length; p++){
            System.out.println(cust[h][p] + ",");
        }
    }
    setrowcount = 0;
    for(int r = 0; r < cust.length; r++){
        if(setrowcount == 0){
            tblmodelll.setRowCount(0);
        }
        try{                
            if(cust[r][0].equals("null") == false){  
                tblmodelll.addRow(cust[r]);  //displays the cust(2D array) data to table 
            }
        } catch(Exception e){
            
        }
        setrowcount++; 
    }
}

我的代码结构中是否缺少某些内容,或者我附加值的逻辑是否不正确?您的回复确实会帮助我解决这个问题。非常感谢。

标签: javacsvuser-interfaceexport-to-csv

解决方案


推荐阅读