【问题标题】:From text file into an arrayList从文本文件到 arrayList
【发布时间】:2016-04-22 19:14:52
【问题描述】:

我正在尝试将文本文件中的数据写入 ArrayList。我有下面的代码。文本文件中的行格式如下:

Id
Car Manufacturer
Car Type
Colour
*blank line*

例如:

21
Vauxhall
Corsa
red

19
Vauxhall
Corsa
blue

18
Vauxhall
Corsa
White

它执行了 24 次,运行时正确显示信息,但列表的大小为“1”。我想知道是否可以通过空格将每辆车分开,以便它们可以单独分开。

【问题讨论】:

  • 空格是空行吧?
  • 请提供文件内容示例,目前很难判断您的问题是什么。例如,您将分隔符定义为 ,\s*,即逗号后跟任何空格,但您的描述没有提及该逗号。
  • 我不明白你的文件格式。您是将idcar manufacturer 等存储在每辆车的一行还是单独的行中?
  • 如果空格空行,也许你可以使用this
  • @thomas 我已更新

标签: java arraylist


【解决方案1】:

您目前仅以逐个值的方式读取文件。可能值得考虑改为逐行读取文件。

所以你可以使用这样的方法:

String lineValue = "";

while (inFile1.hasNextLine()) {
  lineValue = inFile1.nextLine();

  if(lineValue.isEmpty()) {
    temps.add(token1);
    token1 = "";
  } else {
    token1 += lineValue;
  }
}

这将允许它向列表中添加多个单独的字符串。您可能需要在文件末尾包含一个空格来添加最后一个字符串,或者添加额外的代码来检查它是否在文件末尾。

【讨论】:

    【解决方案2】:

    我希望这段代码能解决您的问题:

    public static void main(String[] args) throws IOException {
        FileReader inFile1 = new FileReader(new File("example.txt"));
        List<String> temps = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(inFile1);
        String line = "";
        String propertyText = "";
        while ((line = reader.readLine()) != null) {
            if(line.length() > 0) {
              propertyText += line + "\n";
            }else{
                temps.add(propertyText);
                propertyText = "";
            }
        }
        temps.add(propertyText);
        inFile1.close();
    
        String[] tempsArray = temps.toArray(new String[0]);
    
        for (String s : tempsArray) {
            System.out.println(s);
        }
    
        // this will print the size of this list
        int retval = temps.size();
        System.out.println("Size of list = " + retval);
    }
    

    【讨论】:

    • 谢谢!这正是我正在寻找的。​​span>
    【解决方案3】:

    同意@Thomas。按照代码:

            Scanner inFile1 = new Scanner(new File("example.txt")); // don't use any delimiter 
    
            List<String> temps = new ArrayList<String>();
            String tokenTemp= "";
    
    
            while (inFile1.hasNextLine()) {
            tokenTemp = inFile1.nextLine();
            if(tokenTemp.isEmpty()){
                temps.add(token1);
                token1="";
            }
            else token1 += tokenTemp+" ";
        }
    

    这里插入temps 列表中的每一行。

    【讨论】:

    • 这是我正在寻找的正确步骤。但我想知道是否有可能使每辆车分开存放。所以从 ID 到颜色。
    • 抱歉,我不小心把最后一条评论发布得太早了。我已相应地对其进行了编辑。
    • 请看更新。我没有检查您上次更新的示例输入。
    【解决方案4】:

    这是因为正则表达式会一次性读取整个文件。我这样修改了你的代码,

    Scanner inFile1 = new Scanner(new File("example.txt"));
                //.useDelimiter(",\\s*");
    List<String> temps = new ArrayList<String>();
    while (inFile1.hasNext()) {
        token1 = inFile1.nextLine();
        temps.add(token1);
    }
    

    而且它有效。如果每个新值都打印在新行中,您可以使用 Scanner 类的nextLine() 方法。

    【讨论】:

      【解决方案5】:

      根据您发布的内容,我了解到您使用空行区分每辆车的详细信息,您可以使用以下正则表达式模式在出现两个换行符时进行拆分(一个用于行尾,一个用于空行)

      20
      Vauxhall
      Corsa
      red (newline1)
      (newline2)
      19
      Vauxhall
      Corsa
      blue(newline1)
      (newline2)
      18
      Vauxhall
      Corsa
      White
      

      应该这样做:

      Scanner inFile1 = new Scanner(new File("cars.txt")).useDelimiter("(\r\n){2,}");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 2018-08-13
        • 1970-01-01
        相关资源
        最近更新 更多