zoukankan      html  css  js  c++  java
  • Java解决字节流乱码问题

    1. 转换流

    • InputStreamReader
    • OutputStreamWriter

    2. InputStreamReader 类

    转换流java.io.InputStreamReader,是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。它的字符集可以由名称指定,也可以接受平台的默认字符集。

    构造方法

    • InputStreamReader(InputStream in): 创建一个使用默认字符集的字符流。
    • InputStreamReader(InputStream in, String charsetName): 创建一个指定字符集的字符流。

    3. OutputStreamWriter 类

    转换流java.io.OutputStreamWriter ,是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集。

    构造方法

    • OutputStreamWriter(OutputStream in): 创建一个使用默认字符集的字符流。
    • OutputStreamWriter(OutputStream in, String charsetName): 创建一个指定字符集的字符流。

    4. 读取文件

    使用转换流设置读取编码为UTF-8
    InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"UTF-8");

    package com.se.file;
    
    import java.io.*;
    
    public class Demo06_Fis_乱码 {
        public static void main(String[] args) {
            BufferedReader bfr = null;
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream("F:\Demo1\w.txt"), "UTF-8");
                bfr = new BufferedReader(isr);
                String str = "";
                while ((str = bfr.readLine()) != null) {
                    System.out.println(str);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bfr != null)
                        bfr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    5. 写入文件

    使用转换流设置读取编码为UTF-8

    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:\Demo1\a.txt"), "UTF-8");

    package com.se.file;
    
    import java.io.*;
    
    public class Demo06_Fis_乱码 {
        public static void main(String[] args) {
            BufferedWriter bfw = null;
            try {
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:\Demo1\a.txt"), "UTF-8");
                bfw = new BufferedWriter(osw);
                bfw.write("你好");
                bfw.write("
    ");
                bfw.write("abc");
                bfw.write("
    ");
                bfw.flush();
                bfw.write("哈哈哈");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bfw != null)
                        bfw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    DGL学习(六): GCN实现
    DGL学习(五): DGL构建异质图
    DGL学习(四): 图分类教程
    LuoGuP2495:[SDOI2011]消耗战
    LuoGuP1121:环状最大两段子段和
    LuoGuP3177:[HAOI2015]树上染色
    LuoGuP2607:[ZJOI2008]骑士
    HDU4283:You Are the One
    LuoGuP4294:[WC2008]游览计划
    LuoGuP4127:[AHOI2009]同类分布
  • 原文地址:https://www.cnblogs.com/LEPENGYANG/p/15035266.html
Copyright © 2011-2022 走看看