[英]How to make an array of Strings to store words of a “dictionary” from a .txt file?
I need to make a dictionary that takes words from a .txt
file. 我需要制作一个字典 ,从.txt
文件中提取单词。 These words (separated line by line) need to be stored in a String
array. 这些单词(逐行分隔)需要存储在String
数组中。 I have already gotten to the point of separating the words and adding them to a new .txt
file, but I have no idea how to add them each to a String
array. 我已经知道要分开单词并将它们添加到新的.txt
文件中,但是我不知道如何将它们分别添加到String
数组中。 There are 有
You need to count the lines in the file. 您需要计算文件中的行数。 Create an array of that size. 创建一个该大小的数组 。
Then for each line in the file, read it and insert it into the array at the index[lineReadFrom]
. 然后,对于文件中的每一行, 读取它并将其插入到数组中的index[lineReadFrom]
。
Since you are not allowed to use ArrayList
or LinkedList
objects, I would suggest to save every found word "on the fly" while you are reading the input file. 由于不允许使用ArrayList
或LinkedList
对象,因此建议您在读取输入文件时“即时”保存找到的每个单词。 These is a series of steps you could follow to get this done: 您可以按照以下一系列步骤来完成此操作:
1. Read the file, line by line: Use the common new BufferedReader(new FileInputStream("/path/to/file"))
approach and read line by line (as I assume you are already doing, looking at your code). 1.逐行读取文件:使用常见的new BufferedReader(new FileInputStream("/path/to/file"))
方法并逐行读取(因为我假设您已经在做,请查看代码)。
2. Check every line for words: Break every possilbe word by spaces with a String.split()
and remove punctuation characters. 2.检查每一行是否有单词:用String.split()
将每个possilbe单词用空格String.split()
并删除标点符号。
3. Save every word: Loop through the String array returned by the String.split()
and for every element that you considered a word, update your statistics and write it to your dictionary file with the common new BufferedWriter(new FileWriter("")).write(...);
3.保存每个单词:遍历String.split()
返回的String数组,对于您认为一个单词的每个元素,使用通用的new BufferedWriter(new FileWriter("")).write(...);
4. Close your resources: Close the reader an writer after you finished looping through them, preferably in a finally
block. 4.关闭您的资源:遍历资源后,最好是在finally
块中,关闭读者的作家。
Here is a complete code sample: 这是完整的代码示例:
public static void main(String[] args) throws IOException {
File dictionaryFile = new File("dict.txt");
// Count the number of lines in the file
LineNumberReader lnr = new LineNumberReader(new FileReader(dictionaryFile));
lnr.skip(Long.MAX_VALUE);
// Instantiate a String[] with the size = number of lines
String[] dict = new String[lnr.getLineNumber() + 1];
lnr.close();
Scanner scanner = new Scanner(dictionaryFile);
int wordNumber = 0;
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
if (word.length() >= 2 && !(Character.isUpperCase(word.charAt(0)))) {
dict[wordNumber] = word;
wordNumber++;
}
}
scanner.close();
}
It took about 350 ms
to finish executing on a 118,620 line file, so it should work for your purposes. 完成对118,620行文件的执行大约需要350 ms
,因此它应该可以满足您的目的。 Note that I instantiated the array in the beginning instead of creating a new String[]
on each line (and replacing the old one like you did in your code). 请注意,我在开始时实例化了数组,而不是在每一行上都创建了一个新的String[]
(并像在代码中一样替换了旧的)。
I used wordNumber
to keep track of the current array index so that each word would be added to the array at the right location. 我使用wordNumber
来跟踪当前的数组索引,以便将每个单词添加到数组的正确位置。
I also used .nextLine()
instead of .next()
since you said that the dictionary was separated by line instead of by spaces (which is what .next()
uses). 我还使用.nextLine()
而不是.next()
因为您说字典是用行而不是用空格分隔的( .next()
使用的是字典)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.