0

带缓冲的输入/输入流 - Auci

 2 years ago
source link: https://www.cnblogs.com/Auci/p/16120054.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

BufferedInputStream类 与 BufferedOutputStream类

BufferedInputStream类 可以对所有InputStream的子类进行缓冲区的包装,以达到性能的优化

BufferedOutputStream类 中的 flush()方法 被用来把缓冲区的字节写入到文件中,并清空缓存

BufferedInputStream类的构造方法:

构造方法 介绍

BufferedInputStream(FileInputStream fileInputStream); 创建一个带有32个字节的缓冲输入流

BufferedInputStream(FileInputStream fileInputStream , int size);指定的大小来创建缓冲输入流

BufferedOutputStream类的构造方法:

构造方法 介绍

BufferedOutputStream(FileOutputStream fileOutputStream); 创建一个带有32个字节的缓冲输出流

BufferedOutputStream(FileOutputStream fileOutputStream , int size);指定的大小来创建缓冲输出流

BufferedInputStream类 与 BufferedOutputStream类 实例:

import java.io.*; public class Demo4 {    public static void main(String[] args) {        /**         * 缓冲字节输入流(BufferedInputStream)         * 特点:提高效率         */        File file = new File("C:\\JAVA_API_1.7中文.chm");        BufferedInputStream bufferedInputStream = null;//创建缓冲字节流        FileInputStream fileInputStream = null;        long stare = System.currentTimeMillis();//获得当前流开始时的毫秒值        try {            fileInputStream=new FileInputStream(file);            bufferedInputStream = new BufferedInputStream(fileInputStream);//将文件字节流包装成缓冲字节流            byte by[] = new byte[1024];//缓冲区字节数组(这个缓冲区与Buffered不同)            while ((bufferedInputStream.read(by))!=-1){//使用缓冲字节流读取数据             }            long end = System.currentTimeMillis();//获得当前流结束时的毫秒值            System.out.println("运行经历的毫秒数:"+(end - stare));         } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (bufferedInputStream!=null){                try {                    bufferedInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }                        /**         * 缓冲字节输出流(BufferedOutputStream)         * 特点:提高效率         */        File file1 = new File("C:\\Word.txt");        BufferedOutputStream bufferedOutputStream = null;//创建缓冲字节输出流        FileOutputStream fileOutputStream = null;        try {            fileOutputStream=new FileOutputStream(file1);            bufferedOutputStream=new BufferedOutputStream(fileOutputStream);//将文件输出流包装到缓冲字节输出流             String str = "深山踏红叶,耳畔闻鹭鸣。";            byte by[] = str.getBytes();            bufferedOutputStream.write(by);            //<*> 使用缓冲字节输出流时,要多进行刷新操作,避免等待,有数据时就将数据写入文件当中 <*>            bufferedOutputStream.flush();//刷新(强制将缓冲区数据写入文件中,即使缓冲区没有写满)         } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fileOutputStream!=null){                try {                    fileOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (bufferedOutputStream!=null){                try {                    bufferedOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK