IO处理流(缓冲流)的使用

IO流之过滤流

IO流的继承关系图

这个IO流常见的类。注意可以看到一些的类的继承关系,但是类的构造是不可以乱猜测的,譬如InputStreamReader这个类属于Reader,但它的构造就是传入一个InputStream类型的参数。
InputStreamReader的其中一个构造:
public InputStreamReader(InputStream in, String charsetName){

}
推荐以后尽量使用缓冲流,虽然里面需要传入节点流,但是代码的质量会好一点,而使用基本的流,诸如ByteArrayInputStream,FileInputStream,可能会引发不可控的异常。文件大小,路径,都会成为实践上的坑。ByteArrayInputStream推荐在文件转字节数组的时候使用,而不是仅使用文件复制拷贝。
在这里插入图片描述

1.(字节)文件拷贝,准备的文件为压缩包。

使用的的类主要有:BufferedInputStream,FileInputStream,BufferedOutputStream, FileOutputStream。

1.字节缓冲流BufferedInputStream、BufferedOutputStream可能需要配合byte[]数组的使用。
2.注意流的先开后闭原则
3.刷新操作

import java.io.*;

public class CopyFile01 {
    /**
     * 缓冲流(字节)的使用
     */
    public static void main(String[] args) {
        long one = System.currentTimeMillis();
        BufferedInputStream is = null;
        BufferedOutputStream os = null;

        //文件拷贝的位置
        File end = new File("D:/copy1.zip");
        if (end.exists())
            end.delete();
        //读取文件的位置
        File start = new File("D:/test.zip");
        try{
            if (!start.exists())
                start.createNewFile();
            //读文件
            is =  new BufferedInputStream(new FileInputStream(start));
            //写文件
            os = new BufferedOutputStream(new FileOutputStream(end));
            //操作
            byte[] flush = new byte[1024*10];
            int len ;
            while ((len = is.read(flush)) != -1){
                os.write(flush,0,len);
            }
            os.flush();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
                try {
                    if (is!=null && os!=null){
                        os.close();
                        is.close();
                        long two = System.currentTimeMillis();
                        System.out.println("ok,共耗时"+((two-one)/1000)+"s");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

2.(字符)文件拷贝,准备的文件为txt文本。

使用的类主要有BufferedReader、BufferedWriter、InputStreamReader、OutputStreamWriter、FileInputStream、FileOutputStream。

1.字符缓冲流InputStreamReader和OutputStreamWriter里面的一个构造方法可以设置文件编码的格式。
2.关流的顺序遵循先开后闭的原则。
3.路径最好不要带有中文。
4.可能需要配合char[]数组的使用,只需要设置好数组的大小,编码传输解析等等问题,都在缓冲流帮我们解决掉了。

import java.io.*;

public class CopyFile03 {
    /**
     * 缓冲流(字符)的使用
     * 一般配合char[]数组的使用
     */
    public static void main(String[] args) {
        long l1 = System.currentTimeMillis();
        Reader reader = null;
        Writer writer = null;
        try {
            //原文件
            File start = new File("D:/test.txt");
            File end = new File("D:/testCopy.txt");
            //设置流。其中InputStreamReader参数是InputStream类型。
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(start),"utf-8"));
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(end),"utf-8"));
            //传输操作
            char[] chars = new char[1024*500];
            int read ;
            while (-1 != (read = reader.read(chars))){
                writer.write(chars,0,read);
            }
            //刷新
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关流,顺序注意,最好遵循先开后闭的原则
            try {
                if (null!=reader&&null!=writer) {
                    writer.close();
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                long l2 = System.currentTimeMillis();
                System.out.println("use time : "+(l2-l1)/1000);
            }
        }
    }
}

过滤流的基本格式

字符

注意点:字符流的底层用到了字节流。
总结来说类似,字符缓冲流(基本字符流(字节流))。字符缓冲流里,包一个基本字符流,基本字符流里,在包一个字节流

 //new OutputStreamWriter(OutputStream os)
 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 //new InputStreamReader(InputStream is)
 BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

字节

BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File("D:test.txt")));
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(new File("D:testCopy.txt")));

(1) 后记:超简单的代码实现,容易出现乱码
java代码

public class Demo(){
	File file = new File("e:/test.txt");
	//读取内容
	BufferedReader br = new BufferedReader(new FileReader(file));
	String str = null;
	while( (str=br.readLine())!=null ){
		System.out.println(str);
	}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值