4

OutputStreamWriter介绍&代码实现和InputStreamReader介绍&代码实现 - 夫君

 2 years ago
source link: https://www.cnblogs.com/xuche/p/16460954.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

OutputStreamWriter介绍&代码实现和InputStreamReader介绍&代码实现

java.io.OutputStreamWriter extends Writer

OutputStreamWriter: 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。(编码:把能看懂的变成看不懂)

继续自父类的共性成员方法:

  - void write(int c) 写入单个字符

  - void write(char[] cbuf)写入字符数组

  - abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数

  - void write(String str)写入字符串

  - void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数

  - void flush()刷新该流的缓冲

  - void close() 关闭此流,但要先刷新它

构造方法:

  OutputStreamWriter(OutputStream out)创建使用默认字符编码的 OutputStreamWriter

  OutputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的 OutputStreamWriter

    OutputStream out:字节输出流,可以用来写转换之后的字节到文件中

    String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK,...不指定默认使用UTF-8

使用步骤:

  1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称

  2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)

  3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)

  4.释放资源

public class Demo02OutputStreamWriter {
    public static void main(String[] args) throws IOException {
        //write_utf_8();
        write_gbk();
    }
 
    /*
       使用转换流OutputStreamWriter写GBK格式的文件
    */
    private static void write_gbk() throws IOException {
        //1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"GBK");
        //2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
        osw.write("你好");
        //3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
        osw.flush();
        //4.释放资源
        osw.close();
    }
 
    /*
        使用转换流OutputStreamWriter写UTF-8格式的文件
     */
    private static void write_utf_8() throws IOException {
        //1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("10_IO\\utf_8.txt"),"utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf_8.txt"));//不指定默认使用UTF-8
        //2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
        osw.write("你好");
        //3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
        osw.flush();
        //4.释放资源
        osw.close();
    }
}

java.io.InputStreamReader extends Reader
InputStreamReader:是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。(解码:把看不懂的变成能看懂的)

继承自父类的共性成员方法:

  int read() 读取单个字符并返回

  int read(char[] cbuf)一次读取多个字符,将字符读入数组

  void close() 关闭该流并释放与之关联的所有资源

构造方法:

  InputStreamReader(InputStream in) 创建一个使用默认字符集的 InputStreamReader

  InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的 InputStreamReader

    InputStream in:字节输入流,用来读取文件中保存的字节

    String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK,...不指定默认使用UTF-8

使用步骤:

  1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称

  2.使用InputStreamReader对象中的方法read读取文件

  3.释放资源

注意事项:

  构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码

public class Demo03InputStreamReader {
    public static void main(String[] args) throws IOException {
        //read_utf_8();
        read_gbk();
    }
 
 
    /*
        使用InputStreamReader读取GBK格式的文件
     */
    private static void read_gbk() throws IOException {
        //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        //InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\\gbk.txt"),"UTF-8");//???
        InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"),"GBK");//你好
 
        //2.使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while((len = isr.read())!=-1){
            System.out.println((char)len);
        }
        //3.释放资源
        isr.close();
    }
 
    /*
        使用InputStreamReader读取UTF-8格式的文件
     */
    private static void read_utf_8() throws IOException {
        //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        //InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\\utf_8.txt"),"UTF-8");
        InputStreamReader isr = new InputStreamReader(new FileInputStream("utf_8.txt"));//不指定默认使用UTF-8
        //2.使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while((len = isr.read())!=-1){
            System.out.println((char)len);
        }
        //3.释放资源
        isr.close();
    }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK