简体   繁体   中英

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. These words (separated line by line) need to be stored in a String array. 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. 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] .

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. 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).

2. Check every line for words: Break every possilbe word by spaces with a String.split() and remove punctuation characters.

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(...);

4. Close your resources: Close the reader an writer after you finished looping through them, preferably in a finally block.

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. 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).

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM