【问题标题】:Reading different lines of a txt file into different ArrayList将txt文件的不同行读入不同的ArrayList
【发布时间】:2015-08-26 12:14:18
【问题描述】:

我有一个包含两个不同行的文件,其中包含整数输入。我想将第一行整数读入Arraylist<Integer>,将第二行输入读入其他Arraylist。如何修改以下代码以有效地做到这一点。我无法理解如何使用分隔符。

import java.util.*;
import java.io.*;
public class arr1list {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Integer> list1=new ArrayList<Integer>();
        File file=new File("raw.txt");
        Scanner in=new Scanner(file);
        Scanner.useDelimiter("\\D"); //the delimiter is not working.

        while(in.hasNext())
            list1.add(in.nextInt());
        System.out.println(list1);
        in.close();
    }
}

【问题讨论】:

  • 你能指定确切的要求吗,现在只有2个值如果文件中有多个值怎么办?
  • 整数之间是如何分隔的?
  • 它是如何将数据存储在您的文件中的?你输入什么?预期的结果是什么?你得到什么结果?
  • 如果你想用新行分隔,请使用“\\R”
  • 我的输入文件有两行,每行格式为:“1 23 4 6 7”。数字用空格分隔。

标签: java arraylist java.util.scanner


【解决方案1】:

除了上面的答案,java 8风格

    BufferedReader reader = Files.newBufferedReader(Paths.get("raw.txt"), StandardCharsets.UTF_8);
    List<List<Integer>> output = reader
        .lines()
        .map(line -> Arrays.asList(line.split(" ")))
        .map(list -> list.stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()))
        .collect(Collectors.toList());

因此,您将获得整数列表列表,例如 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 6]]

【讨论】:

    【解决方案2】:

    你能做一些像这样简单的事情吗:

        try (BufferedReader reader = 
                new BufferedReader(new FileReader("path"));) {
    
            List<Integer> first = new ArrayList<>();
    
            for (String number: reader.readLine().split(" ")) {
    
                numbers.add(Integer.parseInt(number));
            }
    
            // do stuff with first and second
    
        } catch (IOException ignorable) {ignorable.printStackTrace();}
    }
    

    BufferedReader.readLine() 将为您处理文件分隔符解析。

    您可以提取需要一行并解析它以创建整数的List 的方法。然后就是像上面那样用reader.readLine()读两行,然后调用方法为每一行生成List。

    【讨论】:

      【解决方案3】:

      我会做这样的事情:

      //Arrays are enough because int is a primitive
      int list1[], list2[];
      
      try {
          Scanner in = new Scanner(new FileReader("file.txt"));
      
          String line1 = (in.hasNextLine()) ? in.nextLine() : "";
          String line2 = (in.hasNextLine()) ? in.nextLine() : "";
      
          String[] line1_values = line1.split(" "); // Split on whitespace
          String[] line2_values = line2.split(" ");
      
          int line1Values[] = new int[line1_values.length], line2Values[] = new int[line2_values.length];
      
          // Map the values to integers
          for(int i = 0; i < line1_values.length; i++)
              line1Values[i] = Integer.parseInt(line1_values[i]);
      
          for(int i = 0; i < line2_values.length; i++)
              line2Values[i] = Integer.parseInt(line2_values[i]);
      
          in.close();      
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
      

      我对此进行了测试,它适用于值由空格分隔的文本文件。

      【讨论】:

        猜你喜欢
        • 2016-05-14
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 2018-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多