歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Eclipse中修改getBytes()的默認編碼格式

Eclipse中修改getBytes()的默認編碼格式

日期:2017/3/1 10:57:38   编辑:Linux編程

Eclipse中編寫程序,String.getBytes()函數的默認編碼格式一般是IDE中工程的Properties設定值。

要在運行時進行修改getBytes()的默認編碼格式,一般使用-Dfile.encoding的運行參數。

代碼樣例如下所示:

  1. void Test02() {
  2. D(System.getProperty("file.encoding"));
  3. try {
  4. String str = "測試";
  5. byte[] bytes = str.getBytes();
  6. printBytes(bytes);
  7. bytes = str.getBytes("utf-8");
  8. printBytes(bytes);
  9. bytes = str.getBytes("gbk");
  10. printBytes(bytes);
  11. bytes = str.getBytes("gb18030");
  12. printBytes(bytes);
  13. System.setProperty("file.encoding", "GBK");
  14. D("/n"+System.getProperty("file.encoding"));
  15. bytes = str.getBytes();
  16. printBytes(bytes);
  17. bytes = str.getBytes("utf-8");
  18. printBytes(bytes);
  19. bytes = str.getBytes("gbk");
  20. printBytes(bytes);
  21. bytes = str.getBytes("gb18030");
  22. printBytes(bytes);
  23. } catch (UnsupportedEncodingException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. void printBytes(byte[] bytes) {
  28. int i=0;
  29. for (byte b : bytes) {
  30. if (i++%10 == 0) D("");
  31. System.out.print("/t" + (0x00ff & b));
  32. }
  33. }
Copyright © Linux教程網 All Rights Reserved