码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA IO ( 字节流转化为字符流 )

时间:2017-01-19 21:26:02      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:reader   stack   ext   exception   创建   ring   try   转化   exce   

public class Index {
    public static void main(String[] args) {
        // 创建文件对象
        File f1 = new File("src/字节流_转化成_字符流/text.txt");
        File f2 = new File("src/字节流_转化成_字符流/textCopy.txt");
        try (
            // 创建输入输出流(java1.7开始在这里面的代码会自动关闭)
            Reader inputStream = new InputStreamReader(new FileInputStream(f1), "GBK");
            Writer outputStream = new OutputStreamWriter(new FileOutputStream(f2, true), "GBK");
        ){
            // 声明缓冲数组
            char[] b = new char[1024];
            // 声明获取字节变量的个数
            int len = -1;
            while ((len = inputStream.read(b)) != -1) {
                // 将读取到的字节数组写入目标文件
                outputStream.write(b, 0, len);
            }
        } catch (Exception e) {
            // 输出异常信息
            e.printStackTrace();
        }
    }
}

 

JAVA IO ( 字节流转化为字符流 )

标签:reader   stack   ext   exception   创建   ring   try   转化   exce   

原文地址:http://www.cnblogs.com/lovling/p/6308680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!