简体   繁体   English

如何创建一个字符串数组来存储.txt文件中“字典”的单词?

[英]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. 由于不允许使用ArrayListLinkedList对象,因此建议您在读取输入文件时“即时”保存找到的每个单词。 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.

相关问题 如何在Android中存储.txt字典文件,并从中检索单词 - How to store .txt dictionary file in android as it is and retrieve words from it 如何创建多维数组以从.txt文件中获取输入并将字符串和数字分开存储 - How to create a multidimensional Array to take input from .txt file and store the strings and the numbers separate 将 .txt 文件中的单词存储到字符串数组中 - Storing words from a .txt file into a String array Java从dictionary.txt文件中获取100个随机单词 - Java get 100 random words from dictionary.txt file 如何从txt文件中读取字符串并将其存储到char数组Java中 - How to read a String from a txt file and store it into a char array java 如何在.txt文件中存储输入数字中的最高数字(以字为单位)? - How do I store in a .txt file the highest number(in words) from input numbers? JAVA-读取.txt文件,混合使用字符串和整数,将数组存储在数组中,将整数存储在2D数组中 - JAVA- Read .txt file with mix of strings & integers, store strings in an array and integers in a 2D array 如何从.txt文件获取多个字符串 - How to get multiple Strings from .txt file 如何从 JTable txt 文件创建字符串? - How to create Strings from a JTable txt file? 将.txt文件中的字符串读取到并将内容放入二维数组 - Read strings from .txt file to and put contents into a 2d array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM