Android-IO流之转换流InputStreamReader/OutputStreamWriter

目录

  1. InputStreamReader/OutputStreamWriter转换流(extends Reader/Writer)
    --9.1 InputStreamReader
    --9.2 OutputStreamWriter
    --9.3 使用OutputStreamWriter和InputStreamReader拷贝文件

9.InputStreamReader/OutputStreamWriter转换流(extends Reader)

将字节流转换成字符流
转换流,用指定码表进行读取/写出

9.1 InputStreamReader

  1. InputStreamReader构造函数
public InputStreamReader(InputStream in)
public InputStreamReader(InputStream in, String charsetName) //charsetName码表名
  1. InputStreamReader重要方法
public int read() throws IOException
public int read(char cbuf[], int offset, int length)

9.2 OutputStreamWriter

1.OutputStreamWriter构造函数

public OutputStreamWriter(OutputStream out, String charsetName) //charsetName码表名
public OutputStreamWriter(OutputStream out)

2.OutputStreamWriter重要方法

public void write(int c)
public void write(char cbuf[], int off, int len)
public void write(String str, int off, int len)
public void flush()

9.3 使用OutputStreamWriter和InputStreamReader拷贝文件

        File dir = Environment.getExternalStorageDirectory();
        File file = new File(dir, "abc.txt");
        File dst = new File(dir, "dst4.txt");

        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);

        FileOutputStream fos = new FileOutputStream(dst);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);

        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        bw.close();
        osw.close();
        fos.close();
        br.close();
        isr.close();
        fis.close();

你可能感兴趣的:(Android-IO流之转换流InputStreamReader/OutputStreamWriter)