zoukankan      html  css  js  c++  java
  • 对于转换流的理解

    一:概述

    转换流:字符流转字节流,字节流转字符流

    使用场景:解决乱码问题

        public static void main(String[] args) throws IOException {
         //源文件为gbk编码
    /*FileReader fr=new FileReader("a.txt"); OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("b.txt"),"utf-8"); int i; while((i=fr.read())!=-1){ osw.write(i); } fr.close(); osw.close();*/
         //源文件为utf-8编码 InputStreamReader isr
    =new InputStreamReader(new FileInputStream("b.txt"),"utf-8"); FileWriter fw=new FileWriter("a.txt"); int j; while((j=isr.read())!=-1){ fw.write(j); } isr.close(); fw.close(); }

    下面为图解编码和解码

    解码:字节流转字符流

    编码:字符流转字节流

    流程:1.读取源文件(gbk)的字节流到内存中,根据平台默认的编码表(gbk)进行解码成字符流

       2.字符流(gbk)编码为字节流(utf-8与目标文件一致),此处使用OutputStreamWriter将gbk转换为utf-8编码

    总结:

    无论是读取的时候,还是写出的时候,都需要参考读取文件和目标文件的编码形式 读取源文件时,源文件的编码需要和解码的编码形式一致 写出到目标文件时,编码形式必须和目标文件的编码形式一致

  • 相关阅读:
    Codeforces Round #610 (Div. 2)C(贪心,思维)
    Educational Codeforces Round 80 (Rated for Div. 2)C(DP)
    BZOJ2190 仪仗队
    BZOJ3613 南园满地堆轻絮
    BZOJ1084 最大子矩阵
    BZOJ1036 树的统计Count
    BZOJ1452 Count
    BZOJ2242 计算器
    BZOJ2705 Longge的问题
    BZOJ1509 逃学的小孩
  • 原文地址:https://www.cnblogs.com/xhlwjy/p/11405139.html
Copyright © 2011-2022 走看看