Java I/O 教程(十二) OutputStreamWriter和InputStreamReader

OutputStreamWriter类

OutputStreamWriter是字符流到字节流的桥梁,字符写入其中后被指定字符集成字节。

字符集可自定义,或使用平台默认字符集。

推荐使用BufferedWriter包装OutputStreamWriter以减少频繁的转换调用,例如:

Writer out
   = new BufferedWriter(new OutputStreamWriter(System.out));
  

类定义

public class OutputStreamWriter
extends Writer

构造函数

OutputStreamWriter(OutputStream out)
Creates an OutputStreamWriter that uses the default character encoding.
OutputStreamWriter(OutputStream out, Charset cs)
Creates an OutputStreamWriter that uses the given charset.
OutputStreamWriter(OutputStream out, CharsetEncoder enc)
Creates an OutputStreamWriter that uses the given charset encoder.
OutputStreamWriter(OutputStream out, String charsetName)
Creates an OutputStreamWriter that uses the named charset.


方法

void

close() 先flush后关闭

void flush() 冲刷流
String getEncoding() 返回流使用的字符集名称
void write(char[] cbuf, int off, int len) 写部分字符数组
void write(int c) 写单个字符
void write(String str, int off, int len) 写部分字符串


例子

package com.dylan.io;

import java.io.*;

/**
 * @author xusucheng
 * @create 2018-01-13
 **/
public class OutputStreamWriterDemo {
    public static void main(String[] args) throws IOException{
        OutputStream w = new FileOutputStream("D:\\testout.txt");
        OutputStreamWriter osw = new OutputStreamWriter(w,"GBK");
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write("欢迎学习java.io");
        bw.flush();
        bw.close();
        System.out.println("写入完成!");
    }
}



InputStreamReader类


InputStreamReader是字节流到字符流的桥梁,它读取字节并使用指定的字符集解码称字符。
字符集可自定义,或使用平台默认字符集。

推荐使用BufferedReader包装InputStreamReader以减少频繁的转换调用,例如:

BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
  

类定义

public class InputStreamReader
extends Reader

构造函数

InputStreamReader(InputStream in)
Creates an InputStreamReader that uses the default charset.
InputStreamReader(InputStream in, Charset cs)
Creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream in, CharsetDecoder dec)
Creates an InputStreamReader that uses the given charset decoder.
InputStreamReader(InputStream in, String charsetName)
Creates an InputStreamReader that uses the named charset.


方法


void close() 
关闭流并释放与其相关资源
String getEncoding()
返回流使用的字符集名称
int read()
读取单个字符
int read(char[] cbuf, int offset, int length)
读取部分字符数组
boolean ready()
判断流是否准备好

例子

package com.dylan.io;

import java.io.*;

/**
 * @author xusucheng
 * @create 2018-01-13
 **/
public class InputStreamReaderDemo {
    public static void main(String[] args) throws IOException{
        InputStream r = new FileInputStream("D:\\testout.txt");
        InputStreamReader isr = new InputStreamReader(r,"GBK");
        BufferedReader br = new BufferedReader(isr);

        if (br.ready()) {
            String content;
            while ((content = br.readLine()) != null) {
                System.out.println(content);
            }
        }

        br.close();
    }
}




posted @ 2018-01-13 17:03  一锤子技术员  阅读(9)  评论(0编辑  收藏  举报  来源