java - 如何在将值附加到 Java 中的 CSV 文件时为每次迭代仅附加一行/行
问题描述
我在将数据附加到我的 CSV 文件方面遇到了一些问题。问题是,每当我第二次尝试将数据附加到我的 CSV 文件中时,附加到 CSV 文件的第二个值与第一个附加值一起出现。就像它在附加到 CSV 文件时带来了现有值。因此,由于这个问题,它会导致此语句中的数组索引越界异常: cust[read2DStringIndex][newVarIndexer] = fromfile[g]; ,CSV 文件的数据重复现有值以及最新的附加值,并且第一个值仅显示在我的 GUI 表上。
桌子:
这是我编写和读取 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++;
}
}
我的代码结构中是否缺少某些内容,或者我附加值的逻辑是否不正确?您的回复确实会帮助我解决这个问题。非常感谢。
解决方案
推荐阅读
- google-cloud-platform - 容器内的 Firestore 事件侦听器冲突
- ansible - CIS 强化 1.1.6 确保 /var 存在单独的分区
- javascript - 查找数组中最昂贵的项目
- julia - 为什么 Julia 比 MATLAB 慢?
- spree - 如何在大礼包中先显示有货的产品,然后再显示无货的产品
- if-statement - 在 csh 的单个 if 条件中检查变量是否未定义或为空
- apache-spark - Spark row_number partitionBy 不按顺序保持自然顺序
- php - 如何使用 PHP 创建并保存到文本文件?
- java - Vert.x RabbitMQ 编码 NullPointerException
- sqlite - 在 sqlite 中创建自增字段