java - 如何读取制表符分隔的 csv 文件的特定列并解析特定列值的所有行值

标签 java

例如,如果我的输入 csv 文件包含

A    B     C     D      E
10   ab    a1    b1     ab1
20   cd    c1    d1     cd1    
30   ef    e1    f1     ef1
40   gh    c1    h1     gh1

我的输出..

Enter a value for C
c1
1)
A | 20
B | cd
C | c1
D | d1
E | cd1
2)
A | 40
B | gh
C | c1
D | h1
E | gh1

最佳答案

首先读取行并按制表符拆分获取第二个索引的值。如果它是用户输入的值,则打印所有值。例如

void cvsRead() throws FileNotFoundException, IOException {

        String line, userinput = "";
        BufferedReader br = new BufferedReader(new FileReader("yourfile.cvs"));
        Scanner scan = new Scanner(System.in);
        System.out.println("enter the column ");
        userinput = scan.nextLine();
        int i = 0;
        String[] spmap = br.readLine().split("\t");
        while ((line = br.readLine()) != null) {

            String[] sp = line.split("\t");
            if (sp[2].equals(userinput)) {
                i++;
                System.out.println(i + ")");
                for (int x = 0; x < spmap.length; x++) {
                    System.out.println(spmap[x] + " | " + sp[x]);
                }
            }

        }
    }

输出>>

enter the column 
c1
1)
A | 20
B | cd
C | c1
D | d1
E | cd1    
2)
A | 40
B | gh
C | c1
D | h1
E | gh1

关于java - 如何读取制表符分隔的 csv 文件的特定列并解析特定列值的所有行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800446/

相关文章:

java - "The type B cannot be a superinterface of C; a superinterface must be an interface"错误

java - m2e-wtp Overlay 未加载到 Eclipse 发布的 EAR 或 WAR 文件中

java - 无法连接到 Watson Platform

java - Tomcat 在启动时自动重启 Web 应用程序,发生错误后

java - 从两个 Mono 对象获取属性,并使用 Reactor Java 将它们设置为第三个对象的属性

javascript - 在完全加载页面之前,加载栏隐藏在jsf中

java - 如何使图像水平反弹?

java - 如何使用 Selenium 阅读 YouTube 评论?

Java Swing Invoice String.format 仅将 for () {} 值返回到 JTextPane?

java - ant 如何编译和 jar 字节相同的 jar 文件,即 MD5 匹配,除非 .java(以及 .class)发生变化?