0

正しくフォーマットするには、大きなテキストファイル(約600 MB)を処理し、フォーマットされた出力を新しいテキストファイルに書き込む必要があります。問題は、新しいファイルへのコンテンツの書き込みが約6.2MBで停止することです。コードは次のとおりです。

/* Analysis of the text in fileName to see if the lines are in the correct format 
     * (Theme\tDate\tTitle\tDescription). If there are lines that are in the incorrect format,
     * the method corrects them.
     */
    public static void cleanTextFile(String fileName, String destFile) throws IOException {
        OutputStreamWriter writer = null;
        BufferedReader reader = null;

        try {
            writer = new OutputStreamWriter(new FileOutputStream(destFile), "UTF8");
        } catch (IOException e) {
            System.out.println("Could not open or create the file " + destFile);
        }

        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("The file " + fileName + " doesn't exist in the folder.");
        }

        String line;
        String[] splitLine;
        StringBuilder stringBuilder = new StringBuilder("");

        while ((line = reader.readLine()) != null) {
            splitLine = line.split("\t");
            stringBuilder.append(line);

            /* If the String array resulting of the split operation doesn't have size 4,
             * then it means that there are elements of the news item missing in the line
             */
            while (splitLine.length != 4) {
                line = reader.readLine();
                stringBuilder.append(line);

                splitLine = stringBuilder.toString().split("\t");
            }
            stringBuilder.append("\n");
            writer.write(stringBuilder.toString());
            stringBuilder = new StringBuilder("");

            writer.flush();
        }

        writer.close();
        reader.close();

    }

私はすでに答えを探しましたが、問題は通常、ライターが閉じられていないという事実またはflush()メソッドの欠如に関連しています。したがって、問題はBufferedReaderにあると思います。私は何が欠けていますか?

4

3 に答える 3

3

このループを見てください:

while (splitLine.length != 4) {
    line = reader.readLine();
    stringBuilder.append(line);

    splitLine = stringBuilder.toString().split("\t");
}

に5つを超えるアイテムが含まれることになった場合は、データを永久に読み取り続けることになります。ファイルの最後に到達しても、に追加しsplitLine続けるだけなので、気付くことさえありません。これが起こっているかどうかはわかりませんが(データがどのように見えるかはわかりません)、確かに実行可能であり、それを防ぐ必要があります。nullStringBuilder

(リソースを閉じるためにもtry/ブロックを使用する必要がありますが、それは別の問題です。)finally

于 2012-10-10T18:34:05.020 に答える
0

FileOutputStreamをそれ自体の変数として分離し、それも閉じます。

FileOutputStream fos = new FileOutputStream(destFile);
writer = new OutputStreamWriter(fos);

   ...

writer.flush();
fos.flush();
于 2012-10-10T18:37:55.843 に答える
0
  1. try / catchは適切にコーディングされていません。エラーが発生した場合、プロセスは続行されます。
  2. 交換できます

        stringBuilder = new StringBuilder("");
    

        stringBuilder.setLength( 0 );
    
  3. line.indexOf('\t',from)代わりに独自のパーサーを使用してくださいString.split()

  4. line.substring(b、e)で取得したパーツをList<String>に追加します
  5. 正しい文字セットでPrintStreamを使用し、2つのパラメーターでコンストラクターを使用します
  6. list.size()> = 4の場合、リストからデータを消費して、情報を4x4で書き込みます。
于 2012-10-10T18:54:50.167 に答える