zoukankan      html  css  js  c++  java
  • 纯文本-FileOutputStream的解码方式

    1.通过string.getBytes(charsetNane)获得的字节数组,字节数组的编码方式,决定了FileOutStream写出文件的格式

    例1:字节数组采用“GBK”编码,write生成的文件也将是“GBK”编码

    package cn.edu.uestc.IO;
    
    import java.io.*;
    
    public class TestFileOutputStream01 {
        public static void main(String[] args){
            //
            File src = new File("abc5.txt");//如果没有文件,自动创建
            //
            OutputStream os = null;
            try {
                os = new FileOutputStream(src,false);
                //操作
                String str = "你好,哈哈哈";
                byte[] bytes = str.getBytes("GBK");//字节数组采用GBK编码
                os.write(bytes,0,bytes.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //释放资源
                try{
                    if (os!=null) {
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    文件类型:

    例2:字节数组采用“UTF-8”编码,write生成的文件也将是“UTF-8”编码

    package cn.edu.uestc.IO;
    
    import java.io.*;
    
    public class TestFileOutputStream02 {
        public static void main(String[] args){
            //创建源
            File src = new File("abc6.txt");
            //生成流
            OutputStream os = null;
            try {
                os = new FileOutputStream(src);
                //操作
                String str = "你好,哈哈哈";
                byte[] bytes = str.getBytes("UTF-8");//字节数组采用"UTF-8"编码
                os.write(bytes,0,bytes.length);
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    if (null!=os){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

     文件类型:

  • 相关阅读:
    How to install Lineage Os ROM on any Android device [2 methods]
    2017 年不可错过的开发工具 Top 50
    (OK) 华为全网通 honor 5x
    (OK) (solved) How restore /cust partition
    (OK) 华为全网通 honor 5x
    (OK) 华为全网通 honor 5x
    Chris Lattner
    (OK) 救砖:华为全网通 honor 5x
    (OK) Linux 平台 CUST.zip 制作方法
    (OK) Linux 下解包华为固件包UPDATE.APP
  • 原文地址:https://www.cnblogs.com/pxb2018/p/10746166.html
Copyright © 2011-2022 走看看