使用 Java 从文本文件中逐列提取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17029721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:45:51  来源:igfitidea点击:

extract data column-wise from text file using Java

javatext

提问by Aman Chawla

I'm working under Java and want to extract data according to column from a text file.
"myfile.txt" contents:

我在 Java 下工作,想根据文本文件中的列提取数据。
“myfile.txt”内容:

    ID     SALARY RANK  
    065    12000   1
    023    15000   2
    035    25000   3
    076    40000   4

I want to extract the data individually according to any Column i.e ID, SALARY, RANK etc
Basically I want to perform operations on individual data according to columns.

我想根据任何列单独提取数据,即 ID、SALARY、RANK 等
基本上我想根据列对单个数据执行操作。

I've listed the data from "myfile.txt" by using while loop and reading line-by-line:

我已经通过使用 while 循环和逐行读取列出了“myfile.txt”中的数据:

    while((line = b.readLine()) != null) {
          stringBuff.append(line + "\n");
       }

link: Reading selective column data from a text file into a list in Java

链接:在 Java 中将文本文件中的选择性列数据读取到列表中

Under bove link it is written to use the following: String[] columns = line.split(" ");

在 bove 链接下,它被编写为使用以下内容: String[] columns = line.split(" ");

But how to use it correctly, please any hint or help?

但是如何正确使用它,请提供任何提示或帮助?

回答by mrcaramori

You can use a regex to detect longer spaces, example:

您可以使用正则表达式来检测更长的空格,例如:

String text = "ID     SALARY RANK\n" +  
            "065    12000   1\n" +
            "023    15000   2\n" +
            "035    25000   3\n" +
            "076    40000   4\n";

Scanner scanner = new Scanner(text);

//reading the first line, always have header
//I suppose
String nextLine = scanner.nextLine();
//regex to break on any ammount of spaces
String regex = "(\s)+";


String[] header = nextLine.split(regex);

//this is printing all columns, you can 
//access each column from row using the array
//indexes, example header[0], header[1], header[2]...
System.out.println(Arrays.toString(header));

//reading the rows
while (scanner.hasNext()) {
    String[] row = scanner.nextLine().split(regex);

    //this is printing all columns, you can 
    //access each column from row using the array
    //indexes, example row[0], row[1], row[2]...
    System.out.println(Arrays.toString(row));
    System.out.println(row[0]);//first column (ID)
}

回答by Majid Laissi

   while((line = b.readLine()) != null) {
      String[] columns = line.split(" ");
      System.out.println("my first column : "+ columns[0] );
      System.out.println("my second column : "+ columns[1] );
      System.out.println("my third column : "+ columns[2] );
   }

Now instead of System.out.println, do whatever you want with your columns.

现在代替System.out.println,对您的列做任何您想做的事情。

But I think your columns are separated by tabsso you might want to use split("\t")instead.

但我认为你的列被分隔,tabs所以你可能想split("\t")改用。