Javaで.txtファイルから文字列をインポートしながら、他のすべての行をスキップ

FinDev:

私は50,000行を持つ.txtファイルから文字列をインポートしようとしています。txtファイル内のすべての単一の行はすべてアンダーケース文字と文字の間にあるスペースを含まない、正確に1つの文字列を持っています。私は以下の書いたコードが動作している、が、1つの奇妙な問題があり、それは.txtファイル(特に偶数行)の他のすべての行をスキップします。いずれかが、私はゴーン間違っをしたところ、私は特定に役立つことができれば、それははるかに高く評価されるだろう。

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;

public class WordRecommender {

StringBuilder sb = new StringBuilder();
               String strLine = "";
               ArrayList<Word> objectArray = new ArrayList<Word>();
               try {
                    BufferedReader br = new BufferedReader(new FileReader("engDictionary.txt"));
                     while (strLine != null)
                      {
                       strLine = br.readLine();
                       sb.append(strLine);
                       sb.append(System.lineSeparator());
                       strLine = br.readLine();
                       if (strLine==null)
                          break;
                       objectArray.add(new Word(strLine));
                   }
                    br.close();
               } catch (FileNotFoundException e) {
                   System.err.println("File not found");
               } catch (IOException e) {
                   System.err.println("Unable to read the file.");
               }
import java.util.ArrayList;

public class Word {

    String wordName;
    ArrayList<Character> uniqueLetters;

    public Word(String string) {
        ArrayList<Character> tempArray = new ArrayList<Character>();

        for (int i = 0; i < string.length(); i++) { 
                        tempArray.add(string.charAt(i));
                    }

        this.wordName = string;
        this.uniqueLetters = tempArray;
}
NPC:

whileループ内では、2本のラインを読み、一つだけを追加しています。あなたは代わりにこれを行うことができます:

while (strLine != null) {
    strLine = br.readLine();
    if (strLine==null)
        break;
    sb.append(strLine);
    sb.append(System.lineSeparator());
    objectArray.add(new Word(strLine));
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=18020&siteId=1
おすすめ