java中的包装流和缓冲流概述
生活随笔
收集整理的這篇文章主要介紹了
java中的包装流和缓冲流概述
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
處理流/包裝流(相對于節(jié)點流更高級)裝飾設(shè)計模式/包裝模式:
1:隱藏了底層的節(jié)點流的差異,并對外提供了更方便的輸入/輸出功能,讓我們只關(guān)心高級流的操作.
2:使用處理流包裝了節(jié)點流,程序直接操作處理流,讓節(jié)點流與底層的設(shè)備做IO操作.
3:只需要關(guān)閉處理流即可.
包裝流如何區(qū)分:寫代碼的時候,發(fā)現(xiàn)創(chuàng)建對象對象的時候,需要傳遞另一個流對象.
new 包裝流( 流對象 ) ;
什么是緩沖流:
是一個包裝流,目的起緩沖作用.BufferedInputStream:BufferedOutputStream:BufferedReader:BufferedWriter:緩沖流的目的:
操作流的時候,習慣定義一個byte/char數(shù)組.
int read():每次都從磁盤文件中讀取一個字節(jié). 直接操作磁盤文件性能極低.
解決方案: 定義一個數(shù)組作為緩沖區(qū).
byte[] buffer = new byte[1024]; 該數(shù)組其實就是一個緩沖區(qū).
一次性從磁盤文件中讀取1024個字節(jié). 如此以來,操作磁盤文件的次數(shù)少了,—->性能得以提升.
既然我們都能想到,SUN公司早就想到了,提供的默認緩存區(qū)大小是8192(1024*8),我們一般不用修改大小.
代碼:
字節(jié)緩沖流案例:
package IO;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;public class BufferStreamDemo {public static void main(String[] args) throws Exception {File file = new File("file/aaa.txt");BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));out.write("中國".getBytes());out.close();BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[1024];int len = -1;while((len = in.read(buffer)) != -1){System.out.println(new String(buffer, 0, len));}in.close();} }字符緩沖流案例:
package IO;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter;public class BufferCharacterDemo {public static void main(String[] args) throws Exception {File file = new File("file/aaa.txt");BufferedWriter in = new BufferedWriter(new FileWriter(file,true));in.newLine();//用來換行等同于‘\n’in.write("美國");in.newLine();in.write("馬來西亞");in.close();BufferedReader out = new BufferedReader(new FileReader(file));String line = null;//按行讀取while((line = out.readLine()) != null){System.out.println(line);}out.close();} }節(jié)點流和緩沖流性能對比:
操作字節(jié)和字符流都習慣使用緩沖流給包裝起來,提供IO性能/效率.
package IO;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream;public class NodeStreamVSBufferStream {public static void main(String[] args) throws Exception {//File file = new File("file/0001.酷六網(wǎng)-余光中《寫給未來的你》(曹可揚朗誦)[流暢版].flv");File file = new File("file/aaa.txt");File copyFile = new File("file/bbb.txt");printContent(file);//File copyFile = new File("file/《寫給未來的你》.flv");test1(file,copyFile);test2(file,copyFile);test3(file,copyFile);test4(file,copyFile);}private static void printContent(File file) throws Exception {BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));byte[] b = new byte[1024];int len = -1;while((len = in.read(b)) != -1){System.out.println(new String(b));}}private static void test4(File file, File copyFile) throws Exception {//測試包裝流 ,1024個字節(jié)拷貝System.out.println("------------測試包裝流 ,1024個字節(jié)拷貝---------------");long start = System.currentTimeMillis();BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(copyFile));int len = -1;byte[] b = new byte[1024];while((len = in.read(b))!= -1){out.write(b);}in.close();out.close();System.out.println(System.currentTimeMillis() - start);}private static void test3(File file, File copyFile) throws Exception{//測試包裝流 ,單個個字節(jié)拷貝System.out.println("------------測試包裝流 ,單個個字節(jié)拷貝---------------");long start = System.currentTimeMillis();BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(copyFile));int len = -1;while((len = in.read())!= -1){out.write(len);}in.close();out.close();System.out.println(System.currentTimeMillis() - start);}private static void test2(File file, File copyFile) throws Exception {//測試字節(jié)流 ,存1024個字節(jié)拷貝System.out.println("------------測試字節(jié)流 ,存1024個字節(jié)拷貝---------------");long start = System.currentTimeMillis();InputStream in = new FileInputStream(file);OutputStream out = new FileOutputStream(copyFile);int len = -1;byte[] b = new byte[1024];while((len = in.read(b))!= -1){out.write(b);}in.close();out.close();System.out.println(System.currentTimeMillis() - start);}private static void test1(File file, File copyFile) throws Exception {//測試一個字節(jié)一個字節(jié)拷貝System.out.println("-----------測試一個字節(jié)一個字節(jié)拷貝----------------");long start = System.currentTimeMillis();InputStream in = new FileInputStream(file);OutputStream out = new FileOutputStream(copyFile);int len = -1;while((len = in.read())!= -1){out.write(len);}in.close();out.close();System.out.println(System.currentTimeMillis() - start);} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java中的包装流和缓冲流概述的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图结构练习——BFSDFS——判断可达性
- 下一篇: 交叉排序