-3
   import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Test {

    List<String> knownWordsArrayList = new ArrayList<String>();
    List<String> wordsArrayList = new ArrayList<String>();
    List<String> newWordsArrayList = new ArrayList<String>();
    String toFile = "";

    public void readKnownWordsFile() {
        try {
            FileInputStream fstream2 = new FileInputStream("knownWords.txt");

            BufferedReader br2 = new BufferedReader(new InputStreamReader(fstream2, "UTF-8"));
            String strLine;
            while ((strLine = br2.readLine()) != null) {
                knownWordsArrayList.add(strLine.toLowerCase());
            }
            HashSet h = new HashSet(knownWordsArrayList);
            // h.removeAll(knownWordsArrayList);
            knownWordsArrayList = new ArrayList<String>(h);
            // for (int i = 0; i < knownWordsArrayList.size(); i++) {
            // System.out.println(knownWordsArrayList.get(i));
            // }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void readFile() {
        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream("Smallville 4x02.de.srt");

            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

            String strLine;

            String numberedLineRemoved = "";
            String strippedInput = "";
            String[] words;
            String trimmedString = "";
            String temp = "";
            // Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                temp = strLine.toLowerCase();
                // Print the content on the console
                numberedLineRemoved = numberedLine(temp);
                strippedInput = numberedLineRemoved.replaceAll("\\p{Punct}", "");
                if ((strippedInput.trim().length() != 0) || (!strippedInput.contains("")) || (strippedInput.contains(" "))) {
                    words = strippedInput.split("\\s+");
                    for (int i = 0; i < words.length; i++) {
                        if (words[i].trim().length() != 0) {
                            wordsArrayList.add(words[i]);
                        }
                    }
                }
            }

            HashSet h = new HashSet(wordsArrayList);
            h.removeAll(knownWordsArrayList);
            newWordsArrayList = new ArrayList<String>(h);

            // HashSet h = new HashSet(wordsArrayList);
            // wordsArrayList.clear();
            // newWordsArrayList.addAll(h);

            for (int i = 0; i < newWordsArrayList.size(); i++) {
                toFile = newWordsArrayList.get(i) + ".\n";
//              System.out.println(newWordsArrayList.get(i) + ".");
                System.out.println();
            }

            System.out.println(newWordsArrayList.size());
            // Close the input stream
            in.close();
        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    public String numberedLine(String string) {
        if (string.matches(".*\\d.*")) {
            return "";
        } else {
            return string;
        }
    }

    public void writeToFile() {
        try {
            // Create file
            FileWriter fstream = new FileWriter("out.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(toFile);
            // Close the output stream
            out.close();
        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.readKnownWordsFile();
        test.readFile();
        test.writeToFile();

    }

}

ファイルから äöüß を読み取るにはどうすればよいですか? string.toLowercase() はこれらも適切に処理しますか? また、äöüß のいずれかを含む単語を印刷する場合、どうすればその単語を適切に印刷できますか? コンソールに出力すると、Außerdem weiß の Außerdem weiŸ が表示されます。これを修正するにはどうすればよいですか?

私は試した:

BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));

しかし今、私は aufklären の代わりに aufkl?ren を取得しており、他の場所でも台無しになっています。

ファイルに正しく印刷されるかどうかを確認するためにコードを更新しましたが、ファイルに 1 つだけ取得しています。

4

1 に答える 1

1

ファイルの作成に使用された文字セットを使用してファイルを読み取る必要があります。Windows マシンを使用している場合、それはおそらく cp1252 です。そう:

BufferedReader br = new BufferedReader(new InputStreamReader(in, "Cp1252"));

それが機能しない場合、ほとんどのテキスト エディターは、特定のドキュメントに使用されているエンコーディングを通知することができます。

于 2013-04-12T19:36:00.067 に答える