代码之家  ›  专栏  ›  技术社区  ›  DanB

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

  •  1
  • DanB  · 技术社区  · 7 年前

    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();
            }
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Denis    7 年前

    您可以将所有行存储在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
  •  0
  •   MiguelKVidal    7 年前

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

    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
  •  0
  •   Nishit Kothari    7 年前

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

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