【问题标题】:Read & split multiple column text file into arrays读取并将多列文本文件拆分为数组
【发布时间】:2017-07-09 18:30:29
【问题描述】:

对于一个项目,我正在处理一个相当大的动物数据集,其中包含多达 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

但我目前无法尝试将每一列拆分为单独的数组,以便我可以处理数据(例如计算方法)。知道我该怎么做吗?任何帮助将不胜感激。

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

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();
        }
    }
}

【问题讨论】:

  • 如果文件是制表符分隔的,那么您需要\t 而不是/t,这可能是它没有按预期工作的原因?
  • 处理完文件后,您将数据存储在何处以及如何存储?在矩阵中(如String[][])?

标签: java arrays split java.util.scanner readfile


【解决方案1】:

您可以将所有行存储在 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];
}

【讨论】:

    【解决方案2】:

    我认为您应该将问题拆分为 2:

    1. 文件读取:

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

      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();
      }
      

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      sn-p 之后会列出给定索引的所有值

      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()));}
      

      【讨论】:

        猜你喜欢
        • 2013-07-23
        • 2016-08-01
        • 1970-01-01
        • 2012-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多