Java输出txt文件为unicode的编码方式



  1. /** 
  2. * @author wx 
  3. *  测试输出流输出的文件类型 
  4. */  
  5. public class IOtest {  
  6.   
  7.     private BufferedOutputStream os1;  
  8.     private OutputStreamWriter os2;  
  9.       
  10.     /** 
  11.      * @param fileName 
  12.      * 输出文件(字节流) 
  13.      */  
  14.     private void outputFile1(String fileName){  
  15.         File f = new File(fileName);  
  16.         try {  
  17.             os1 = new BufferedOutputStream(new FileOutputStream(f));  
  18.             os1.write("测试".getBytes(""));  
  19.         } catch (FileNotFoundException e) {  
  20.             e.printStackTrace();  
  21.         } catch (UnsupportedEncodingException e) {  
  22.             e.printStackTrace();  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }finally{  
  26.             try {  
  27.                 os1.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.     }  
  33.       
  34.     /** 
  35.      * @param fileName 
  36.      * 输出文件(字符流) 
  37.      */  
  38.     private void outputFile2(String fileName){  
  39.         File f = new File(fileName);  
  40.         try {  
  41.             os2 = new OutputStreamWriter(new FileOutputStream(f),"Unicode");  
  42.             os2.write("测试");  
  43.         } catch (FileNotFoundException e) {  
  44.             e.printStackTrace();  
  45.         } catch (UnsupportedEncodingException e) {  
  46.             e.printStackTrace();  
  47.         } catch (IOException e) {  
  48.             e.printStackTrace();  
  49.         }finally{  
  50.             try {  
  51.                 os2.close();  
  52.             } catch (IOException e) {  
  53.                 e.printStackTrace();  
  54.             }  
  55.         }  
  56.     }  
  57.       
  58.     public static void main(String[] args) {  
  59.         new IOtest().outputFile1("f:/workspace/test1.txt");  
  60.         new IOtest().outputFile2("f:/workspace/test2.txt");  
  61.     }  
  62. }  
复制代码
需要test1.TXT的编码方式为Unicode,此种方式为Unicode big endian。
  1. os1 = new BufferedOutputStream(new FileOutputStream(f));  
  2. byte[] bom={-1, -2};  
  3. os1.write(bom);  
  4. os1.write("测试".getBytes("UTF-16LE"));
复制代码
JAVA内部使用的unicode是UTF-16BE的,当上面的getBytes中的字符集指定为: 鱼尾纹怎么去除http://www.bj-swjtu.com/ywwcz/
  1. /** 
  2. * @author wx 
  3. *  测试输出流输出的文件类型 
  4. */  
  5. public class IOtest {  
  6.   
  7.     private BufferedOutputStream os1;  
  8.     private OutputStreamWriter os2;  
  9.       
  10.     /** 
  11.      * @param fileName 
  12.      * 输出文件(字节流) 
  13.      */  
  14.     private void outputFile1(String fileName){  
  15.         File f = new File(fileName);  
  16.         try {  
  17.             os1 = new BufferedOutputStream(new FileOutputStream(f));  
  18.             os1.write("测试".getBytes(""));  
  19.         } catch (FileNotFoundException e) {  
  20.             e.printStackTrace();  
  21.         } catch (UnsupportedEncodingException e) {  
  22.             e.printStackTrace();  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }finally{  
  26.             try {  
  27.                 os1.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.     }  
  33.       
  34.     /** 
  35.      * @param fileName 
  36.      * 输出文件(字符流) 
  37.      */  
  38.     private void outputFile2(String fileName){  
  39.         File f = new File(fileName);  
  40.         try {  
  41.             os2 = new OutputStreamWriter(new FileOutputStream(f),"Unicode");  
  42.             os2.write("测试");  
  43.         } catch (FileNotFoundException e) {  
  44.             e.printStackTrace();  
  45.         } catch (UnsupportedEncodingException e) {  
  46.             e.printStackTrace();  
  47.         } catch (IOException e) {  
  48.             e.printStackTrace();  
  49.         }finally{  
  50.             try {  
  51.                 os2.close();  
  52.             } catch (IOException e) {  
  53.                 e.printStackTrace();  
  54.             }  
  55.         }  
  56.     }  
  57.       
  58.     public static void main(String[] args) {  
  59.         new IOtest().outputFile1("f:/workspace/test1.txt");  
  60.         new IOtest().outputFile2("f:/workspace/test2.txt");  
  61.     }  
  62. }  
复制代码
需要test1.TXT的编码方式为Unicode,此种方式为Unicode big endian。
  1. os1 = new BufferedOutputStream(new FileOutputStream(f));  
  2. byte[] bom={-1, -2};  
  3. os1.write(bom);  
  4. os1.write("测试".getBytes("UTF-16LE"));
复制代码
JAVA内部使用的unicode是UTF-16BE的,当上面的getBytes中的字符集指定为: 
  UTF-16或unicode时,文件输出为UTF-16BE,而且本件内包含两个字节的BOM(Byte Order Marker)信息。 
   UTF-16BE或UTF-16LE时,文件输出为分别为UTF-16BE和UTF-16LE,但文件内不包含BOM信息。 
   当NODEPAD(记事本)打开文件时会自己判断BOM信息,来以合适的方式打开文件,其中它里面的unicode就是UTF-16LE,unicode big endian就是UTF-16BE。 
   所以,为了记事本显示为unicode,需要人为的加入BOM信息,如上面的: 
  1. byte[] bom={-1, -2};  
  2. os1.write(bom);  

鱼尾纹怎么去除
  UTF-16或unicode时,文件输出为UTF-16BE,而且本件内包含两个字节的BOM(Byte Order Marker)信息。 
   UTF-16BE或UTF-16LE时,文件输出为分别为UTF-16BE和UTF-16LE,但文件内不包含BOM信息。 
   当NODEPAD(记事本)打开文件时会自己判断BOM信息,来以合适的方式打开文件,其中它里面的unicode就是UTF-16LE,unicode big endian就是UTF-16BE。 
   所以,为了记事本显示为unicode,需要人为的加入BOM信息,如上面的: 
  1. byte[] bom={-1, -2};  
  2. os1.write(bom);  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值