2013-04-12 2 views
-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ß for Außerdem weiß 어떻게 해결할 수 있습니까? 대신 aufklären 그뿐만 아니라 다른 곳에서 망치의 르네자바에서 äöüß를 어떻게 읽을 수 있습니까?

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

을하지만 지금은 aufkl 받고 있어요?

나는 시도했다.

파일에 제대로 인쇄 될지 확인하기 위해 코드가 업데이트되었지만 파일에 하나만 표시됩니다.

+1

무엇이 있습니까? .. –

+5

유니 코드와 UTF-8 또는 UTF-16에 대해 자세히 알아보십시오. – duffymo

+0

파일에서 파일을 읽음으로써 파일에서 읽을 수 있습니다. 이 질문은 말이되지 않습니다. –

답변

1

파일을 만드는 데 사용 된 charset을 사용하여 파일을 읽어야합니다. Windows 시스템을 사용 중이라면 아마도 cp1252 일 것입니다. 그래서 : 그래도 문제가 해결되지 않으면

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

, 대부분의 텍스트 편집기는 주어진 문서에 사용되는 인코딩을 알려주 할 수있다.

관련 문제