嗨,我正在尝试从.csv文件中获取特定数据,而我使用的代码是

import java.io.BufferedReader;
import java.io.FileReader;

public class InsertValuesIntoTestDb {


    public static void main(String[] args) throws Exception {


        String splitBy = ",";

        BufferedReader br = new BufferedReader(newFileReader("test.csv"));


        String line = br.readLine();

        while ((line = br.readLine()) !=null) {

             String[] b = line.split(splitBy);

             System.out.println(b[0]);
        }

        br.close();

  }

}


.csv看起来像

a,f,w,b,numinst,af,ub

1RW,800,64,22,1,48:2,true

1RW,800,16,39,1,48:2,true

1RW,800,640,330,1,48:2,true

1RW,800,40,124,1,48:2,true

1RW,800,32,104,1,48:2,true

1RW,800,8,104,1,48:2,true

1R1W,800,65536,39,1,96:96,true

1R1W,800,2048,39,1,96:96,true

1R1W,800,8192,39,1,48:48,true


使用上面的代码,我只能打印列“ a”,而我的输出看起来像

a


1RW

1Rw

1Rw


像这样,但是我怎么能打印f,w,b列呢?

我的输出应该像

f   w   b

800 64 22

800 64 22

800 64 22


谢谢

最佳答案

只需将System.out.println(b[0]);更改为System.out.println(b[1]+" "+b[2]+" "+b[3]);

关于java - 如何使用Java从CSV打印多列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31020508/

10-11 01:33