Re: [問題] 如何印出奇怪的字
※ 引述《missbus (smile)》之銘言:
: 比如說"!"倒過來的符號
: "?"倒過來的符號
: 或者是"é"
: 在java都不能成功顯示出來
: 小弟用的OS: vista sp1
: jre1.6.0_05 with eclipse
: 在run的時候
: 會跑出錯誤
: Save could not be completed.
: Reason:
: Some characters cannot be mapped using "MS950" character encoding.
: Either change the encoding or remove the characters which are not supported
: by the "MS950" character encoding.
: 到preference設定Default encoding為utf-8
: 印出來都是問號
: 請問板上的大大有何解決的方法呢....?
首先要感謝GERRYccc和LPH66 firedragen 大大們的指教
這個問題終於解決了
小弟在這做一下整理
===============================
問題一開始是"é"無法在 java 裡用 console 印出來
原因出在 encoding 上面
不論是 Text file encoding 或者是 Console Encoding
在 eclipse 裡面預設的 encoding 是MS950
偏偏"é"無法 由MS950 成功 encoding
所以會出現
Save could not be completed.
Reason:
Some characters cannot be mapped using "MS950" character encoding.
Either change the encoding or remove the characters which are not supported
by the "MS950" character encoding.
所以我就在 eclipse 裡面更改有關 encoding 的設定
1. Windows > Preferences... > General > Content Types
裡面的 Java Class File 和 Text 的 Default encoding 都改成 UTF-8 並 Update
2.Windows > Preferences... > General > Editors > Text Editors > Spelling
Encoding 也改成 UTF-8
3.Windows > Preferences... > General > Workspace
裡面的Text file encoding改成 UTF-8
4.把 .java 檔用記事本打開並另存新檔編碼成 unicode
接著回到eclipse按 F5 重新整理
原本以為設定那麼多應該是可以成功顯示"é"
很不幸的失敗了
在 console 視窗裡出現的是"?"
當下是一整個挫折感超重這樣
回到 C_and_CPP 版看到 LPH66 大說
" "é"輸出到 console 上是轉成 console 的字集
所以輸出到 console 上那就有可能顯示不出來"
這時就想如何設定 console 視窗 output 的 encoding...
就在這時候 GERRYccc 大給了一個不錯的網址
http://www.javaworld.com.tw/jute/post/print?bid=5&id=221496
我就照著裡面的指示做
5.Run > Open Debug Dialog > Common
裡面的 Console encoding 改成 UTF-8
如果這樣做就成功的話那該有多好XD
可惜上面那網址有提到說因eclipse沒有整合好的緣故
就算把 Console encoding 換成 UTF-8 後輸出未必正確
我有試了一下 果然還是亂碼...
還好他有給解決方案
6.在程式碼加入
System.setOut(new PrintStream(System.out, true, "UTF-8"));
System.setErr(new PrintStream(System.err, true, "UTF-8"));
目的是要確保Java 應用在執行時, JRE 的預設 encoding 是 UTF-8
這兩行程式碼 run 過一次後就可以把它移除
經過上面6個步驟洗禮以後 小弟就成功解決了此問題
說實在還頗高興的說XDXDDD..... 以上....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.161.187.213
※ 編輯: missbus 來自: 118.161.187.213 (04/21 05:03)
→
04/21 05:04, , 1F
04/21 05:04, 1F
推
04/21 20:07, , 2F
04/21 20:07, 2F
推
04/21 23:00, , 3F
04/21 23:00, 3F
→
04/21 23:03, , 4F
04/21 23:03, 4F
推
04/21 23:06, , 5F
04/21 23:06, 5F
討論串 (同標題文章)