java IO

学习了java 的io 自己总结下,总共可以用到的有两类,字节流和字符流。

字节流主要有 以Stream结尾的流,包括,InputStream、OutputStream、BufferedInputStream、BufferedOutputStream

                                                               FileInputStream、 FileOutputStream

字符流主要以 Reader结尾的流     包括     FileOutputReader、 FileInputReader BufferedReader、BufferedWriter

以及包括字节流到字符流的转换,进行解码编码 InputStreamReader OutputStreamWriter

package com.ssm.test;

import org.junit.Test;

import java.io.*;

/**
 * @Author: zhouheng
 * @Created: with IntelliJ IDEA.
 * @Description:
 * @Date: 2018-03-21
 * @Time: 23:11
 */
public class TestOther {

    @Test
    public void test() {
        BufferedWriter bw = null;
        BufferedReader br = null;

        try {
            File file = new File("e:/logs/log.log");
            File file2 = new File("e:/logs/log3.log");

//            读取字符流
            FileInputStream fis = new FileInputStream(file);
//            转换字节流
            InputStreamReader isr = new InputStreamReader(fis, "GBK");
//            设置缓冲流
            br = new BufferedReader(isr);

            FileOutputStream fos = new FileOutputStream(file2);
            OutputStreamWriter osr = new OutputStreamWriter(fos, "GBK");
            bw = new BufferedWriter(osr);

            String str;
            int len;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
                bw.write(str + "\n");
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


这个程序主要是以字符流的形式读入文件,然后转换为字节流,然后再进行缓存流的读取,速度较快。

字节流可以读取任意的文件,声音视频,而字符流只可以读取文本文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值