【问题标题】:Reading a text file (~90,000 words) and trying to add each word into an ArrayList of strings读取文本文件(约 90,000 个单词)并尝试将每个单词添加到字符串的 ArrayList 中
【发布时间】:2017-05-03 00:43:43
【问题描述】:

我的方法读取并打印文件,但我无法将每个单词添加到ArrayList dict

阅读器一次读取一个字符,所以我写的内容将每个字符添加到dict: [c,a,t,d,o,g] 当我想要 [cat,dog] 时。文本文件在自己的行上有单词;如何区分它们?

到目前为止我的代码:

public static List Dictionary() {
    ArrayList <String> dict = new ArrayList <String>(); 

    File inFile = new File("C:/Users/Aidan/Desktop/fua.txt");   
    FileReader ins = null;

    try {
        ins = new FileReader(inFile);

        int ch;

        while ((ch = ins.read()) != -1) {
            System.out.print((char) ch);

            dict.add((char) ch + "");
        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            ins.close();
        } catch (Exception e) {
        }
    }
    return dict;
}

【问题讨论】:

  • 假设有很好的例子和方法来解决这个问题。请关注post

标签: java string arraylist filereader tokenize


【解决方案1】:

请遵守 Java 命名约定,因此请使用 readDictionary 而不是 Dictionary(看起来像类名)。接下来,我会将fileName 传递给方法(而不是在方法中硬编码路径)。我不会重新发明轮子,而是使用Scanner。您也可以在此处使用try-with-resources 代替finally(以及菱形运算符)。喜欢,

public static List<String> readDictionary(String fileName) {
    List<String> dict = new ArrayList<>();

    try (Scanner scan = new Scanner(new File(fileName))) {
        while (scan.hasNext()) {
            dict.add(scan.next());
        }
    } catch (Exception e) {
        System.out.printf("Caught Exception: %s%n", e.getMessage());
        e.printStackTrace();
    }
    return dict;
}

或者,自己使用BufferedReadersplit 每个词。喜欢,

public static List<String> readDictionary(String fileName) {
    List<String> dict = new ArrayList<>();

    try (BufferedReader br = new BufferedReader(new FileReader(
                new File(fileName)))) {
        String line;
        while ((line = br.readLine()) != null) {
            if (!line.isEmpty()) {
                Stream.of(line.split("\\s+"))
                        .forEachOrdered(word -> dict.add(word));
            }
        }
    } catch (Exception e) {
        System.out.printf("Caught Exception: %s%n", e.getMessage());
        e.printStackTrace();
    }
    return dict;
}

但这基本上是第一个示例所做的。

【讨论】:

    【解决方案2】:

    在此处查看答案,该答案显示了如何使用 Scanner 从文件中获取单词:Read next word in java

    您不想打印出单词,而是希望将它们附加到 ArrayList。

    【讨论】:

      【解决方案3】:

      由于FileReaderread 方法一次只能读取一个字符,而这不是您想要的,那么我建议您使用Scanner 来读取文件。

      ArrayList<String> dict = new ArrayList<>(); 
      Scanner scanner = new Scanner(new File("C:/Users/Aidan/Desktop/fua.txt"));
      while(scanner.hasNext()){
           dict.add(scanner.next());   
      }
      

      【讨论】:

        【解决方案4】:

        您可以将您的FileReader 包装在BufferedReader 中,它有一个readLine() 方法,可以一次获取一整行(单词)。 readLine() 在没有更多行可读取时返回 null

        【讨论】:

          猜你喜欢
          • 2016-02-12
          • 2012-06-08
          • 2017-09-27
          • 1970-01-01
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 2014-11-07
          相关资源
          最近更新 更多