Java GBK,UTF-8编码

本文通过示例代码详细介绍了GBK与UTF-8两种编码方式的区别与使用方法,并强调了编码与解码的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



1、GBK,UTF-8编码

注意:一般默认的是GBK编码。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package io.dol.sn;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStreamWriter;  
  6.   
  7. public class EnCodeStream {  
  8.   
  9.     public static void main(String[] args) throws IOException {  
  10.           
  11.         //WriteGBK();  
  12.         WriteUTF_8();  
  13.     }  
  14.     public static void WriteGBK() throws IOException  
  15.     {  
  16.         //默认是GBK编码  
  17.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"));  
  18.         //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("UTF_8.txt","GBK"));  
  19.         //注意,此时产生的gbk.txt文件大小为4字节  
  20.         osw.write("海豚");  
  21.         osw.close();  
  22.     }  
  23.     public static void WriteUTF_8() throws IOException  
  24.     {  
  25.         //默认是GBK编码  
  26.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("UTF_8.txt"),"UTF-8");  
  27.         //注意,此时产生的UTF_8.txt文件大小为6字节  
  28.         osw.write("海豚");  
  29.         osw.close();  
  30.     }  
  31. }  

2、编码解码

用什么格式编码,就用什么格式解码;编码一次,解码一次。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package io.dol.sn;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Arrays;  
  5.   
  6. public class EnCodeStream {  
  7.   
  8.     public static void main(String[] args) throws IOException {  
  9.           
  10.         String s1 = "海豚";  
  11.         //编码:  
  12.         //以下一样,因为默认就是GBK编码  
  13.         byte[] bGbk1 = s1.getBytes();  
  14.         byte[] bGbk2 = s1.getBytes("GBK");  
  15.         //Arrays.toString()将字符数组转化为字符串  
  16.         System.out.println(Arrays.toString(bGbk1));  
  17.         System.out.println(Arrays.toString(bGbk2));  
  18.         //解码:  
  19.         String s2 = new String(bGbk1);  
  20.         String s3 = new String(bGbk2,"GBK");  
  21.         //如果用UTF-8解码,则会出现解码格式错误  
  22.         //String s4 = new String(bGbk2,"UTF-8");  
  23.         System.out.println(s2);  
  24.         System.out.println(s3);  
  25.         //这里会打印出“????”  
  26.         //System.out.println(s4);  
  27.     }  
  28. }  

3、GBK,UTF-8都支持中文编码

有兴趣的可以去试一下:在记事本中写入“联通”二字,保存后再打开会出现什么现象,为什么呢?


原文地址:http://blog.youkuaiyun.com/qingdujun/article/details/41366301

1、GBK,UTF-8编码

注意:一般默认的是GBK编码。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package io.dol.sn;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStreamWriter;  
  6.   
  7. public class EnCodeStream {  
  8.   
  9.     public static void main(String[] args) throws IOException {  
  10.           
  11.         //WriteGBK();  
  12.         WriteUTF_8();  
  13.     }  
  14.     public static void WriteGBK() throws IOException  
  15.     {  
  16.         //默认是GBK编码  
  17.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"));  
  18.         //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("UTF_8.txt","GBK"));  
  19.         //注意,此时产生的gbk.txt文件大小为4字节  
  20.         osw.write("海豚");  
  21.         osw.close();  
  22.     }  
  23.     public static void WriteUTF_8() throws IOException  
  24.     {  
  25.         //默认是GBK编码  
  26.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("UTF_8.txt"),"UTF-8");  
  27.         //注意,此时产生的UTF_8.txt文件大小为6字节  
  28.         osw.write("海豚");  
  29.         osw.close();  
  30.     }  
  31. }  

2、编码解码

用什么格式编码,就用什么格式解码;编码一次,解码一次。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package io.dol.sn;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Arrays;  
  5.   
  6. public class EnCodeStream {  
  7.   
  8.     public static void main(String[] args) throws IOException {  
  9.           
  10.         String s1 = "海豚";  
  11.         //编码:  
  12.         //以下一样,因为默认就是GBK编码  
  13.         byte[] bGbk1 = s1.getBytes();  
  14.         byte[] bGbk2 = s1.getBytes("GBK");  
  15.         //Arrays.toString()将字符数组转化为字符串  
  16.         System.out.println(Arrays.toString(bGbk1));  
  17.         System.out.println(Arrays.toString(bGbk2));  
  18.         //解码:  
  19.         String s2 = new String(bGbk1);  
  20.         String s3 = new String(bGbk2,"GBK");  
  21.         //如果用UTF-8解码,则会出现解码格式错误  
  22.         //String s4 = new String(bGbk2,"UTF-8");  
  23.         System.out.println(s2);  
  24.         System.out.println(s3);  
  25.         //这里会打印出“????”  
  26.         //System.out.println(s4);  
  27.     }  
  28. }  

3、GBK,UTF-8都支持中文编码

有兴趣的可以去试一下:在记事本中写入“联通”二字,保存后再打开会出现什么现象,为什么呢?


原文地址:http://blog.youkuaiyun.com/qingdujun/article/details/41366301

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值