高效字节流bufferedinputstream和bufferedoutputstream

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aaa958099161/article/details/78288530
public class MyBuffer {

public static void main(String[] args) throws IOException{

//在这里定义,后面finally关闭

BufferedInputStream bufferedInputStream=null;
BufferedOutputStream bufferedOutputStream=null;

try{

//其实就是装饰着模式,内部调用的还是fileinputstream和fileoutputstream

bufferedInputStream=new BufferedInputStream(new FileInputStream("IO.txt"));
bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("buffer.txt"));

int len=0;

//写入,这里是读取很多字节,然后一起写入

while((len=bufferedInputStream.read())!=-1){
bufferedOutputStream.write(len);
}}catch(Exception e){

e.printStackTrace();
}finally{
if(bufferedInputStream!=null){
bufferedInputStream.close();}
if(bufferedInputStream!=null){
bufferedOutputStream.close();
}
}

}
阅读更多
博主设置当前文章不允许评论。
换一批

BufferedInputStreamBufferedOutputStream的源代码

10-07

谁能掰开了,揉碎了,解读一下BufferedInputStream和BufferedOutputStream的源代码,为什么要来这一个类?黑马程序员培训营毕向东老师讲到这时,没有具体讲到它怎么就提高效率了。rn[code=java]package com.itheima.io.copy;rnrnimport java.io.BufferedInputStream;rnimport java.io.BufferedOutputStream;rnimport java.io.FileInputStream;rnimport java.io.FileNotFoundException;rnimport java.io.FileOutputStream;rnimport java.io.IOException;rnrnpublic class CopyTextBufTest rnrn /**rn * @param argsrn * @throws IOExceptionrn */rn public static void main(String[] args) throws IOException rnrn // 以源文件作为参数,建立文件字节输入流对象rnrn FileInputStream fis = new FileInputStream(rn "I:\\20151002锵锵三人行 作家的钱是那里来的【华夏视讯网首发hxsxw.com】【迅雷+百度云下载】.rmvb");rnrn // 建立文件字节输入流中转站对象rnrn BufferedInputStream bufis = new BufferedInputStream(fis);rnrn // 以目标文件作为参数,建立文件字节输出流对象rnrn FileOutputStream fos = new FileOutputStream(rn "G:\\20151002锵锵三人行 作家的钱是那里来的.rmvb");rnrn // 建立文件字节输出流中转站对象rnrn BufferedOutputStream bufos = new BufferedOutputStream(fos);rn // “读取”“写入”文件第一种方式rn // 定义存储单个字节的变量rnrn // int single_byte=0;rnrn // 调用文件字节输入流中转站对象“读取”功能rnrn // while((single_byte=bufis.read())!=-1)rnrn // 调用文件字节输出流中转站对象“写入”功能rnrn // bufos.write(single_byte);rn // rnrn // “读取”“写入”文件第二种方式rn // 定义一个字节数组rnrn byte[] buf = new byte[1024];rnrn // 定义一个记录获取了多少个字节的变量rnrn int len = 0;rnrn // 调用文件字节输入流中转站对象“读取”功能rnrn while ((len = bufis.read(buf)) != -1) rnrn // 调用文件字节输出流中转站对象“写入”功能rnrn bufos.write(buf, 0, len);rn rn rnrnrn[/code]rn这个是毕老师的代码(思路是这样的,代码不完全一样),看了几遍,唯有一个感受,绕来绕去,有这个必要吗?rn老是说着“提高效率”“提高效率”,可是不知道为什么总有一种感觉,就是这和我没关系!也是,我从没有直观地感受到所谓的“提高效率”!

关于BufferedInputStreamBufferedOutputStream的问题

05-01

[code=java]public class Copypicture rnpublic static void main(String[] args) rn copy_2();//copy_1();rnrnpublic static void copy_1()rn FileInputStream fis = null;rn FileOutputStream fos = null;rn try rn fis = new FileInputStream("1.doc");rn fos = new FileOutputStream("2.doc");rn byte[] buff = new byte[1024];rn int len = 0;rn while((len=fis.read(buff))!=-1)rn //System.out.println(len);rn fos.write(buff,0,len);rn rn System.out.println("success!");rn catch (FileNotFoundException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finally rn try rn fis.close();rn fos.close();rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rnrnpublic static void copy_2()rn BufferedInputStream bis = null;rn BufferedOutputStream bos = null;rn //int i = 0;rn try rn bis = new BufferedInputStream(new FileInputStream("1.doc"));rn bos = new BufferedOutputStream(new FileOutputStream("2.doc"));rn //byte[] buff = new byte[1024];rn int len = 0;rn while((len=bis.read())!=-1)//return getBufIfOpen()[pos++] & 0xff;rn //System.out.println(i++);rn bos.write(len);//文件有多字节就写多少次rn rn System.out.println("success!");rn catch (FileNotFoundException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finally rn try rn bis.close();rn bos.close();rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rnrnrn[/code]rn问题是:rncopy_1()方法和copy_2()方法有什么区别?我理解的是copy_1()方法是从1.doc文件中读1024字节大小的文本到内存中,然后一股脑写入2.doc,而copy_2()方法是读1024字节大小的文本到内存中,然后一个字节一个字节的写入到2.doc中,是这样吗?可是如果是这样,那BufferedOutputStream这个定义了干嘛,效率不是反而低了吗?

没有更多推荐了,返回首页