JavaSE 高级 第13节 字符输出流OutputStreamWriter

2016-07-24

Reader

InputStreamReader

Writer

OutputStreamWriter

package com.java1995;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class TestOutputStreamWriter {

    public static void main(String[] args) {

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;

        try {
            fos = new FileOutputStream("D:\\workspace\\java_io\\out.txt");
            osw = new OutputStreamWriter(fos, "UTF-8");
            String str = "大家好,很高兴见到各位。Hello ,nice to meet you!";
            for (int i = 0; i < str.length(); i++) {
                osw.write(str.charAt(i));
            }
            osw.flush();//这一步很重要
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 关闭流
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

【参考资料】

[1] Java轻松入门经典教程【完整版】

 

posted @ 2016-07-24 17:54  岑亮  阅读(223)  评论(0编辑  收藏  举报