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

mac2026-08-11  8

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")));
最新回复(0)