zoukankan      html  css  js  c++  java
  • java字节流和字符流编码格式(转自姚刚)

    /**
    * 字节流和字符流的区别,
    * (1)字符流带有缓冲,必须flush或close后数据才会从缓存中写入磁盘文件。
    * 字节流直接写文件。
    * (2)字符流在创建流的时候,指定编码,字节流在文件输出的时候指定编码
    * jdk中哪些是字符流哪些是字节流,一句话,以Writer、Reader结尾的都是字符流,以Stream结尾的都是字节流。
    * (3)字符流都是对字节流的封装
    *
    */

    //当前测试所用系统为中文windows XP 系统 默认GB2312编码

    //**************************写操作*********************************

    /*FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\test\ok.txt"));
    fileOutputStream.write("联通".getBytes());//系统默认编码
    */

    /*FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\test\ok.txt"));
    fileOutputStream.write("联通".getBytes("UTF-8"));*/

    /* OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\test\ok.txt")); //系统默认编码
    outputStreamWriter.write("联通");
    outputStreamWriter.close();*/

    /*OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\test\ok.txt"),"UTF-8");
    outputStreamWriter.write("联通");
    outputStreamWriter.close();*/

    /*OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\test\ok.txt"),"UTF-8");
    outputStreamWriter.write("联通");
    outputStreamWriter.close();*/

    //****************************读取操作******************************

    /*FileInputStream fileInputStream = new FileInputStream(new File("C:\test\ok.txt"));
    byte[] readValue = new byte[1024];
    fileInputStream.read(readValue);
    System.out.println(new String(readValue)); //系统默认方式解码*/


    /*FileInputStream fileInputStream = new FileInputStream(new File("C:\test\ok.txt"));
    byte[] readValue = new byte[1024];
    fileInputStream.read(readValue);
    System.out.println(new String(readValue,"UTF-8"));*/

    /*InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("C:\test\ok.txt")); //系统默认解码
    char[] content = new char[1024];
    inputStreamReader.read(content);
    System.out.println(content);*/

    /*InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("C:\test\ok.txt"),"UTF-8"); //系统默认解码
    char[] content = new char[1024];
    inputStreamReader.read(content);
    System.out.println(content);*/

  • 相关阅读:
    电脑桌面图标不显示图案变成白色图标该怎么办?
    行动上的巨人养成指南
    荣耀MagicBook Pro 2020款对比2019款做了哪些升级
    P8 function template ,函数模板
    MATLAB中imfill()函数
    MagicBook Pro 2020锐龙版
    P7. class template, 类模板,模板类
    6. namespace经验谈
    macbook pro2020参数
    MATLAB取整
  • 原文地址:https://www.cnblogs.com/mu-tou-man/p/5763692.html
Copyright © 2011-2022 走看看