读取多列文本文件并将其拆分为数组

Read & split multiple column text file into arrays

对于一个项目,我正在处理一个相当大的动物数据集,其中包含多达 14 个数据参数。我能够读取它并使用它显示为字符串:

public static void readIn(String file) throws IOException {

    Scanner scanner = new Scanner(new File(file));
    while (scanner.hasNext()) {
        String[] columns = scanner.nextLine().split("/t");
        String data = columns[columns.length-1];
        System.out.println(data);
    }
}

并显示如下内容:

04:00:01    0.11    0.04    -0.1    1047470 977.91  91.75
04:00:01    0.32    -0.03   -0.07   1047505 977.34  92.91
04:00:01    0.49    -0.03   -0.08   1047493 978.66  92.17

但我目前在尝试将每一列拆分为单独的数组以便处理数据(例如计算均值)时遇到问题。知道我该怎么做吗?任何帮助将不胜感激。

编辑: 谢谢,我找到了一个可行的解决方案,还可以让我选择它具体读取的频道。我还决定将数据作为数组存储在 class 中,这就是我现在拥有的:

public static void readChannel(String file, int channel) throws IOException 
{
    List<Double> dataArr = new ArrayList<>();
    Scanner scanner = new Scanner(new File(file));
    while (scanner.hasNext()) {
        String[] columns = scanner.nextLine().split("\t");

        for (int i = channel; i < columns.length; i+=(columns.length-channel)) {
            dataArr.add(Double.parseDouble(columns[i]));
            dataArr.toArray();
        }
    }
}

您可以将所有行存储在 ArrayList 中,然后为每一列创建数组并将值存储在其中。示例代码:

Scanner scanner = new Scanner(new File(file));
ArrayList<String> animalData = new ArrayList<String>();
while (scanner.hasNext()) {
    String[] columns = scanner.nextLine().split("/t");
    String data = columns[columns.length-1];
    animalData.add(data);
    System.out.println(data);
}

int size = animalData.size();
String[] arr1 = new String[size]; String[] arr2 = new String[size]; 
String[] arr3 = new String[size]; String[] arr4 = new String[size];
for(int i=0;i<size;i++)
{
    String[] temp = animalData.get(i).split("\t");
    arr1[i] = temp[0];
    arr2[i] = temp[1];
    arr3[i] = temp[2];
    arr4[i] = temp[3];
}

我认为你应该把你的问题分成两部分:

  1. 文件读取:

    您的程序读取每一行并将其保存在您定义的 class 实例中:

    public class MyData {
        private String time;
        private double percent;
        //... and so on
    }
    public MyData readLine( String line ) {
        String[] columns = line.split("\t");
        MyData md = new MyData();
        md.setTime( columns[ 0 ] );
        md.setPercent( Double.parseDouble(columns[ 1 ]) );
    }
    public void readFile( File file ) {
        Scanner scanner = new Scanner(file);
        List<MyData> myList = new ArrayList<>();
        while (scanner.hasNext()) {
            MyData md = readLine( scanner.nextLine() );
            myList.add( md );
        }
    }
    
  2. 数据处理:

    处理文件后,您可以创建处理数据所需的方法:

    int sum = 0;
    for ( MyData md : myList ) {
        sum = sum + md.getValue();
    }
    

希望对您有所帮助。

以下代码段将列出给定索引的所有值

public static void readIn(String file) throws Exception {

Scanner scanner = new Scanner(new File(file));
  final Map<Integer,List<String>> resultMap = new HashMap<>();
  while (scanner.hasNext()) {
    String[] columns = scanner.nextLine().split("/t");
    for(int i=0;i<columns.length;i++){
      resultMap.computeIfAbsent(i, k -> new ArrayList<>()).add(columns[i]);
    }
  }   resultMap.keySet().forEach(index -> System.out.println(resultMap.get(index).toString()));}