Eclipse中修改getBytes()的默认编码格式
Eclipse中修改getBytes()的默认编码格式
Eclipse中编写程序,String.getBytes()函数的默认编码格式一般是IDE中工程的Properties设定值。
要在运行时进行修改getBytes()的默认编码格式,一般使用-Dfile.encoding的运行参数。
代码样例如下所示:
- void Test02() {
- D(System.getProperty("file.encoding"));
- try {
- String str = "测试";
- byte[] bytes = str.getBytes();
- printBytes(bytes);
- bytes = str.getBytes("utf-8");
- printBytes(bytes);
- bytes = str.getBytes("gbk");
- printBytes(bytes);
- bytes = str.getBytes("gb18030");
- printBytes(bytes);
- System.setProperty("file.encoding", "GBK");
- D("/n"+System.getProperty("file.encoding"));
- bytes = str.getBytes();
- printBytes(bytes);
- bytes = str.getBytes("utf-8");
- printBytes(bytes);
- bytes = str.getBytes("gbk");
- printBytes(bytes);
- bytes = str.getBytes("gb18030");
- printBytes(bytes);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- void printBytes(byte[] bytes) {
- int i=0;
- for (byte b : bytes) {
- if (i++%10 == 0) D("");
- System.out.print("/t" + (0x00ff & b));
- }
- }
评论暂时关闭