读取文本文件(约 90,000 个单词)并尝试将每个单词添加到字符串的 ArrayList 中
Reading a text file (~90,000 words) and trying to add each word into an ArrayList of strings
我的方法读取并打印文件,但我无法将每个单词添加到 ArrayList dict
。
reader 一次读取文件一个字符,所以我写的是将每个字符添加到 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;
}
查看此处显示如何使用扫描仪从文件中获取单词的答案:Read next word in java。
您不想打印出单词,而是想将它们附加到 ArrayList。
由于FileReader
的read
方法一次只能读取一个字符,这不是你想要的,那我建议您使用 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());
}
您可以将您的 FileReader
包装在 BufferedReader
中,它有一个 readLine()
方法可以让您一次得到整行(单词)。 readLine()
returns null
当没有更多行可读时。
请遵守 Java 命名约定,因此 readDictionary
而不是 Dictionary
(看起来像 class 名称)。接下来,我会将 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;
}
或者,每个词自己使用 BufferedReader
和 split
。喜欢,
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;
}
但这基本上就是第一个示例所做的。
我的方法读取并打印文件,但我无法将每个单词添加到 ArrayList dict
。
reader 一次读取文件一个字符,所以我写的是将每个字符添加到 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;
}
查看此处显示如何使用扫描仪从文件中获取单词的答案:Read next word in java。
您不想打印出单词,而是想将它们附加到 ArrayList。
由于FileReader
的read
方法一次只能读取一个字符,这不是你想要的,那我建议您使用 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());
}
您可以将您的 FileReader
包装在 BufferedReader
中,它有一个 readLine()
方法可以让您一次得到整行(单词)。 readLine()
returns null
当没有更多行可读时。
请遵守 Java 命名约定,因此 readDictionary
而不是 Dictionary
(看起来像 class 名称)。接下来,我会将 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;
}
或者,每个词自己使用 BufferedReader
和 split
。喜欢,
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;
}
但这基本上就是第一个示例所做的。