2013-05-10 2 views
-1

인코딩 된 문자열을 Cp1252 인코딩으로 저장하려고 시도하고 자바 텍스트 영역에 표시합니다. 다시 읽을 때 특수 문자가 일반적으로있는 물음표가있는 검은 색 다이아몬드가 있습니다 (', & 등). 적절한 문자를 표시하기 위해 포맷하려면 어떻게해야합니까?자바 인코딩 오류

단어가 밖으로 나왔을 때 텍스트가 거의 올바르게 표시 될 때 텍스트를 복사하여 붙여 넣을 수 없습니다. 그러나 Cp1252 파일을 읽으려는 코드는 다음과 같습니다.

try { 
     br = new BufferedReader(new FileReader(f)); 
     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      emi.stringContent += "\n" + strLine; 
     } 

    br.close(); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(EMailTmpDirRead.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(EMailTmpDirRead.class.getName()).log(Level.SEVERE, null, ex); 
    } 

고마워요! 다음 편집을 제작

지금은 아무것도 읽하지

StringBuffer temp2 = new StringBuffer(1000); 
    int numRead = 0; 
    char[] buf = new char[1024]; 

    try { 
     ir = new InputStreamReader(new FileInputStream(f), "Cp1252"); 
     while((numRead = ir.read()) != -1) 
     { 
      String temp = String.valueOf(buf, 0, numRead); 
      temp2.append(temp); 
      buf = new char[1024]; 
     } 
     emi.stringContent = temp2.toString(); 

라인이 StringBuffer와의 TEMP2 = 새로운 StringBuffer를() 생략 한 것으로 나타났습니다.;

try { 
     ir = new InputStreamReader(new FileInputStream(f), "Cp1252"); 
     br = new BufferedReader(ir); 
     while(br.readLine() != null) 
     { 
      temp2.append(br.readLine()); 
     } 
     emi.stringContent = temp2.toString(); 
+0

가능한 복제본 [Java FileReader 인코딩 문제] (http://stackoverflow.com/questions/696626/) java-filereader-encoding-issue) –

답변

0

오히려 FileReader를 사용하는 것보다, InputStreamReader로 싸서 FileInputStream을 사용한다; InputStreamReader의 생성자에서 문자 인코딩을 지정하십시오 (this page에 따르면 인코딩에 "Cp1252"를 사용해야하는 것처럼 보입니다)

+0

캔트가 여전히 그것을 얻는 것처럼 보입니다 ... 그것을 읽는 루프는 무엇입니까? – parkjohnston

+0

edit ... 도움을 청하십시오 – parkjohnston

+0

[BufferedReader] (http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html)에서'InputStreamReader '를 래핑 해보고 루프 내의 BufferedReader의'readLine()'메소드 ('readLine()'이 null을 읽었을 때 루프를 종료). –