1

我编写了一个从 CSV 文件读取数据的小型 Java 程序,我必须将这些值保存在二维数组中。现在我需要将此数组中的这些值(并非所有信息都将被存储,因为数组包含许多冗余数据)写入一个新的 CSV 文件。任何人都可以帮助我提供代码示例,我搜索了很多但找不到答案。我的代码是:

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++;
}
4

1 回答 1

0

你可以使用这样的东西......

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();
于 2013-10-21T11:29:28.883 回答