java byte缓存_Java 之 字节缓冲流
一、字節(jié)緩沖輸出流
java.io.BufferedOutputStream extends OutputStream
BufferedOutputStream:字節(jié)緩沖輸出流。
繼承自父類的共性成員方法:
public void close() :關(guān)閉此輸出流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源。
public void flush() :刷新此輸出流并強(qiáng)制任何緩沖的輸出字節(jié)被寫出。
public void write(byte[] b):將 b.length字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流。
public void write(byte[] b, int off, int len) :從指定的字節(jié)數(shù)組寫入 len字節(jié),從偏移量 off開始輸出到此輸出流。
public abstract void write(int b) :將指定的字節(jié)輸出流。
構(gòu)造方法:
BufferedOutputStream(OutputStream out) 創(chuàng)建一個(gè)新的緩沖輸出流,以將數(shù)據(jù)寫入指定的底層輸出流。
BufferedOutputStream(OutputStream out, int size) 創(chuàng)建一個(gè)新的緩沖輸出流,以將具有指定緩沖區(qū)大小的數(shù)據(jù)寫入指定的底層輸出流。
參數(shù):
OutputStream out:字節(jié)輸出流,可以傳遞 FileOutputStream,緩沖流會給FileOutputStream增加一個(gè)緩沖區(qū),提高FileOutputStream的寫入效率
int size:指定緩沖流內(nèi)部緩沖區(qū)的大小,不指定默認(rèn)。
使用步驟【重要】
①?創(chuàng)建FileOutputStream對象,構(gòu)造方法中綁定要輸出的目的地
②?創(chuàng)建BufferedOutputStream對象,構(gòu)造方法中傳遞FileOutputStream對象對象,提高FileOutputStream對象效率
③?使用BufferedOutputStream對象中的方法write,把數(shù)據(jù)寫入到內(nèi)部緩沖區(qū)中
④?使用BufferedOutputStream對象中的方法flush,把內(nèi)部緩沖區(qū)中的數(shù)據(jù),刷新到文件中
⑤?釋放資源(會先調(diào)用flush方法刷新數(shù)據(jù))
Demo:
1 public static void main(String[] args) throwsIOException {2 //1.創(chuàng)建FileOutputStream對象,構(gòu)造方法中綁定要輸出的目的地
3 FileOutputStream fos = new FileOutputStream("E:\\a.txt");4 //2.創(chuàng)建BufferedOutputStream對象,構(gòu)造方法中傳遞FileOutputStream對象對象,提高FileOutputStream對象效率
5 BufferedOutputStream bos = newBufferedOutputStream(fos);6 //3.使用BufferedOutputStream對象中的方法write,把數(shù)據(jù)寫入到內(nèi)部緩沖區(qū)中
7 bos.write("我把數(shù)據(jù)寫入到內(nèi)部緩沖區(qū)中".getBytes());8 //4.使用BufferedOutputStream對象中的方法flush,把內(nèi)部緩沖區(qū)中的數(shù)據(jù),刷新到文件中
9 bos.flush();10 //5.釋放資源(會先調(diào)用flush方法刷新數(shù)據(jù),第4部可以省略)
11 bos.close();12 }
二、字節(jié)緩沖輸入流
java.io.BufferedInputStream extends InputStream
BufferedInputStream:字節(jié)緩沖輸入流
繼承自父類的成員方法:
int read()從輸入流中讀取數(shù)據(jù)的下一個(gè)字節(jié)。
int read(byte[] b) 從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲在緩沖區(qū)數(shù)組 b 中。
void close() 關(guān)閉此輸入流并釋放與該流關(guān)聯(lián)的所有系統(tǒng)資源。
構(gòu)造方法:
BufferedInputStream(InputStream in) 創(chuàng)建一個(gè) BufferedInputStream 并保存其參數(shù),即輸入流 in,以便將來使用。
BufferedInputStream(InputStream in, int size) 創(chuàng)建具有指定緩沖區(qū)大小的 BufferedInputStream 并保存其參數(shù),即輸入流 in,以便將來使用。
參數(shù):
InputStream in:字節(jié)輸入流,可以傳遞FileInputStream,緩沖流會給FileInputStream增加一個(gè)緩沖區(qū),提高FileInputStream的讀取效率
int size:指定緩沖流內(nèi)部緩沖區(qū)的大小,不指定默認(rèn)。
使用步驟【重要】:
①?創(chuàng)建FileInputStream對象,構(gòu)造方法中綁定要讀取的數(shù)據(jù)源
②?創(chuàng)建BufferedInputStream對象,構(gòu)造方法中傳遞FileInputStream對象,提高FileInputStream對象的讀取效率
③?使用BufferedInputStream對象中的方法read,讀取文件
④?釋放資源
Demo:
1 public static void main(String[] args) throwsIOException {2 //1.創(chuàng)建FileInputStream對象,構(gòu)造方法中綁定要讀取的數(shù)據(jù)源
3 FileInputStream fis = new FileInputStream("10_IO\\a.txt");4 //2.創(chuàng)建BufferedInputStream對象,構(gòu)造方法中傳遞FileInputStream對象,提高FileInputStream對象的讀取效率
5 BufferedInputStream bis = newBufferedInputStream(fis);6 //3.使用BufferedInputStream對象中的方法read,讀取文件7 //int read()從輸入流中讀取數(shù)據(jù)的下一個(gè)字節(jié)。
8 /*int len = 0;//記錄每次讀取到的字節(jié)9 while((len = bis.read())!=-1){10 System.out.println(len);11 }*/
12
13 //int read(byte[] b) 從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲在緩沖區(qū)數(shù)組 b 中。
14 byte[] bytes =new byte[1024];//存儲每次讀取的數(shù)據(jù)
15 int len = 0; //記錄每次讀取的有效字節(jié)個(gè)數(shù)
16 while((len = bis.read(bytes))!=-1){17 System.out.println(new String(bytes,0,len));18 }19
20 //4.釋放資源
21 bis.close();22 }
總結(jié)
以上是生活随笔為你收集整理的java byte缓存_Java 之 字节缓冲流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: java adt mac_Mac下搭建E
 - 下一篇: java stringtoarray_j