2012-04-18 99 views
0

我試圖從系統控制檯讀取流程,但是我在編碼方面存在一些問題。 因爲我是法國人,所以我的鍵盤有一些特殊的字符,例如'é','è','à'等等......Java - 編碼字符串

當我把控制檯的內容放到一個文本區域時,這樣的事情:

Le volume dans le lecteur C s'appelle DisqueC 
Le num?ro de s?rie du volume est CE7........ 
3 fichier(s)   20?229 octets 
5 R?p(s) 450?096?623?616 octets libres 

它automaticaly取代特殊字符的'?'而且尺寸也有問題。

我讀的控制檯類似的內容:緩衝區是通過套接字發送

BufferedReader stdInput = new BufferedReader(new InputStreamReader(child.getInputStream(), "UTF-8")); 
BufferedReader stdError = new BufferedReader(new InputStreamReader(child.getErrorStream(), "UTF-8")); 

String line; 
byte[] lineBytes; 
while ((line = stdInput.readLine()) != null) 
{ 
    lineBytes = line.getBytes("UTF-8"); 
    buffer.append("\t\t" + new String(lineBytes, "UTF-8") + "\n"); 
} 
stdInput.close(); 
while ((line = stdError.readLine()) != null) 
{ 
lineBytes = line.getBytes("UTF-8"); 
buffer.append("\t\t" + new String(lineBytes, "UTF-8") + "\n"); 
} 
stdError.close(); 

然後我展示它就像進入一個JTextArea:

textArea.append(new String(console_output.getBytes(), "UTF-8");); 

所以,任何人都可以告訴我爲什麼我的編碼有這個問題?

謝謝。

+1

這可能是相關的:http://stackoverflow.com/questions/2914069/how-to-add-diffrent-characterset-support-for- jtextarea – mcfinnigan 2012-04-18 09:39:43

回答

1

如果你是在Windows上做的,你應該忘記這一點。默認的命令行編碼ist CP850是廢話。您可以更改該會話,但不能調用任何批處理文件。

嘗試chcp 65001

這將切換到UTF-8。檢查這個StackOveflow search

+0

即使您使用chcp 65001將代碼頁更改爲UTF-8,控制檯也可能無法正確顯示UTF-8。請參閱http://stackoverflow.com/questions/10143998/cyrillic-in-windows-consolejava-system-out-println/10147081#10147081 – dragon66 2012-04-18 13:14:45

+0

可惜,Windows控制檯是純粹的廢話。 – 2012-04-18 19:24:12

0

在某些情況下,這種功能將是很好:

System.out.println(String.format(Locale.UK, <your String value>, args));