?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package cn.test;
import java.io.UnsupportedEncodingException;
public class Demo15 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "你好ABC123" ;
byte [] b1 = str.getBytes(); //转换成字节系列用的是项目默认的编码
for ( byte b : b1) {
//把字节(转换成了int)以十六进制方式显示
System.out.print(Integer.toHexString(b & 0xff ) + " " );
}
System.out.println( "" );
//utf8编码,中文占用3个字节,英文和数字占用1个字节
byte [] b2 = str.getBytes( "utf8" );
for ( byte b : b2) {
System.out.print(Integer.toHexString(b & 0xff ) + " " );
}
System.out.println( "" );
//gbk编码,中文占用两个字节,英文和数字占用1个字节
byte [] b3 = str.getBytes( "gbk" );
for ( byte b : b3) {
System.out.print(Integer.toHexString(b & 0xff ) + " " );
}
System.out.println( "" );
//java是双字节编码 utf-16be
//utf-16be编码,中文占2个字节,英文和数字也占用2个字节
byte [] b4 = str.getBytes( "utf-16be" );
for ( byte b : b4) {
System.out.print(Integer.toHexString(b & 0xff ) + " " );
}
System.out.println( "" );
//当字节序列是某种编码时,这时候想把字节序列变成字符串,也需要用这种编码方式,否则会出现乱码
String str1 = new String(b4); //使用项目默认的编码
System.out.println(str1);
String str2 = new String(b4, "utf-16be" );
System.out.println(str2);
}
}
|
执行结果:
?
1
2
3
4
5
6
|
e4 bd a0 e5 a5 bd 41 42 43 31 32 33
e4 bd a0 e5 a5 bd 41 42 43 31 32 33
c4 e3 ba c3 41 42 43 31 32 33
4f 60 59 7d 0 41 0 42 0 43 0 31 0 32 0 33
O`Y}ABC123
你好ABC123
|
如果我们在中文机器上直接创建文本文件,那么该文本文件只认识ansi编码(中文系统下,ansi编码代表gbk编码)
好了,以上所述是小编给大家介绍的Java IO流 文件的编码的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
原文链接:http://www.cnblogs.com/tianxintian22/archive/2017/05/05/6813085.html
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 53
-
2025-05-27 70
-
2025-05-29 59
-
2025-05-25 92
-
2025-05-29 37
热门评论