• 纯文本-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();
                }
            }
        }
    }

     文件类型:

  • 相关阅读:
    test
    c# cook book -Linq 关于Object的比较
    关于UnitOfWork
    autofac学习
    webapi 开启跨域支持
    httpclient通过post提交到webapi
    jQuery之元素查找
    jQuery之过滤元素
    jQuery之回到顶部
    jQuery之_元素滚动
  • 原文地址:https://www.cnblogs.com/pxb2018/p/10746166.html
Copyright © 2020-2023  润新知