用java将多维数组中的值写入csv文件
我写了一个小Java程序,从CSV文件中读取数据,我必须将这些值保存在二维数组中。现在我需要将这些值(并非所有信息都将被存储,因为数组包含许多冗余数据)从该数组写入一个新的CSV文件。有人能帮我一个代码示例吗?我搜索了很多,但找不到答案。我的代码是:用java将多维数组中的值写入csv文件,java,csv,multidimensional-array,Java,Csv,Multidimensional Array,我写了一个小Java程序,从CSV文件中读取数据,我必须将这些值保存在二维数组中。现在我需要将这些值(并非所有信息都将被存储,因为数组包含许多冗余数据)从该数组写入一个新的CSV文件。有人能帮我一个代码示例吗?我搜索了很多,但找不到答案。我的代码是: int row = 0; int col = 0; String[][] numbers=new String[24][24]; File file = new File("D:\\thesis\\sorted_file.csv"); if(fi
int row = 0;
int col = 0;
String[][] numbers=new String[24][24];
File file = new File("D:\\thesis\\sorted_file.csv");
if(file.exists())
{
System.out.println("file exist");
}
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String line = null;
String delims=",";
//read each line of text file
while((line = bufRdr.readLine()) != null )
{
StringTokenizer st = new StringTokenizer(line,delims);
col=0;
while (st.hasMoreTokens())
{
//get next token and store it in the array
numbers[row][col] = st.nextToken();
System.out.print("number["+row+"]["+col+"]:"+numbers[row][col]);
col++;
}
row++;
}
你可以用这样的东西
BufferedWriter br = new BufferedWriter(new FileWriter("myfile.csv"));
StringBuilder sb = new StringBuilder();
for (String element : numbers) {
sb.append(element);
sb.append(",");
}
br.write(sb.toString);
br.close();
那么问题出在哪里呢?我试着使用它,但它在br.write(sb.toString)上有错误;错误是(toString不能解析或不是字段),另一个错误是{for(String元素:number)}number不能解析为变量