Java 读取文本文件(~90000个单词),并尝试将每个单词添加到字符串的ArrayList中

Java 读取文本文件(~90000个单词),并尝试将每个单词添加到字符串的ArrayList中,java,string,arraylist,filereader,tokenize,Java,String,Arraylist,Filereader,Tokenize,我的方法读取并打印文件,但在将每个单词添加到ArrayList dict时遇到问题 读取器一次读取一个字符,因此我所写的内容会在我需要[cat,dog]时将每个字符添加到dict:[c,a,t,d,o,g]。文本文件中的单词位于其自己的行上;我怎样才能区分它们 到目前为止,我的代码是: public static List Dictionary() { ArrayList <String> dict = new ArrayList <String>();

我的方法读取并打印文件,但在将每个单词添加到
ArrayList dict
时遇到问题

读取器一次读取一个字符,因此我所写的内容会在我需要[cat,dog]时将每个字符添加到
dict
:[c,a,t,d,o,g]。文本文件中的单词位于其自己的行上;我怎样才能区分它们

到目前为止,我的代码是:

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;
}
公共静态列表字典(){
ArrayList dict=新的ArrayList();
File infle=新文件(“C:/Users/Aidan/Desktop/fua.txt”);
FileReader-ins=null;
试一试{
ins=新文件读取器(infle);
int-ch;
而((ch=ins.read())!=-1){
系统输出打印((字符)ch);
dict.add((char)ch+“”);
}
}捕获(例外e){
系统输出打印ln(e);
}最后{
试一试{
ins.close();
}捕获(例外e){
}
}
返回命令;
}

在此处查看答案,其中显示了如何使用扫描仪从文件中获取单词:


与其打印单词,不如将它们附加到ArrayList中。

在此处查看答案,其中显示了如何使用扫描仪从文件中获取单词:


与其打印单词,不如将它们附加到ArrayList。

因为
文件阅读器的
read
方法一次只能读取一个字符,而这不是您想要的,那么我建议您使用
扫描仪来读取文件

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

由于
文件阅读器的
读取
方法一次只能读取一个字符,这不是您想要的,因此我建议您使用
扫描仪
读取文件

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

您可以将
文件读取器
包装在
缓冲读取器
中,该读取器具有一个
readLine()
方法,可以一次获取整行(字)<当没有更多行可读取时,code>readLine()
返回
null

您可以将
文件读取器
包装在
缓冲读取器
中,该读取器有一个
readLine()
方法,可以一次获取整行(字)<当没有更多行可读取时,code>readLine()
返回
null

请遵守Java命名约定,因此
readDictionary
而不是
Dictionary
(看起来像类名)。接下来,我将把
文件名
传递到方法中(而不是在方法中硬编码路径)。我不会再发明轮子,我会用一个新的。您还可以在此处使用
try with resources
而不是
finally
(以及菱形操作符)。像


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

请遵守Java命名约定,因此
readDictionary
而不是
Dictionary
(看起来像类名)。接下来,我将把
文件名
传递到方法中(而不是在方法中硬编码路径)。我不会再发明轮子,我会用一个新的。您还可以在此处使用
try with resources
而不是
finally
(以及菱形操作符)。像


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

假设有很好的例子和方法来解决这个问题。假设有很好的例子和方法来解决这个问题,请参考以下内容。参考以下内容
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;
}