10.4-全棧Java筆記:常用流詳解(二)

上節(jié)我們講到「Java中常用流:文件字節(jié)流和字節(jié)數(shù)組流」,本節(jié)我們繼續(xù)走起~

?緩沖流

BufferedInputStream和BufferedOutputStream這兩個流是處理流,通過內(nèi)部緩存數(shù)組來提高操作流的效率。

【示例1】使用緩沖流實現(xiàn)文件的高效率復(fù)制操作

public?class?Demo06 {

????public?static?void?main(String[]?args) {

????????new?Demo06().copyFile("d:/a.txt",?"d:/b.txt");

??? }

????void?copyFile(String?src,String?dec){

??????? FileInputStream?fis?=?null;

??????? BufferedInputStream?bis?=?null;

??????? FileOutputStream?fos?=?null;

??????? BufferedOutputStream?bos?=?null;

????????int?temp?= 0;

????????try?{

?????????????fis?=?new?FileInputStream(src);

?????????????bis?=?new?BufferedInputStream(fis);

?????????????fos?=?new?FileOutputStream(dec);

?????????????bos?=?new?BufferedOutputStream(fos);

?????????????while((temp=bis.read())!=-1){

?????????????????bos.write(temp);

???????????? }

??????? }?catch?(Exception?e) {

?????????????e.printStackTrace();

??????? }?finally{

??????????//********增加處理流后,注意流的關(guān)閉順序!“后開的先關(guān)閉!”

?????????????try?{

?????????????????if(bos!=null){

?????????????????????bos.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

??? ????????try?{

?????????????????if(bis!=null){

?????????????????????bis.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

?????????????try?{

?????????????????if(fos!=null){

?????????????????????fos.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

?????????????try?{

?????????????????if(fis!=null){

?????????????????????fis.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

??????? }

??? }

}

示例1 運行結(jié)果

數(shù)據(jù)流將“基本數(shù)據(jù)類型變量”作為數(shù)據(jù)源,從而允許程序以與機器無關(guān)方式從底層輸入輸出流中操作java基本數(shù)據(jù)類型。??

DataInputStream和DataOutputStream提供了可以存取與機器無關(guān)的所有Java基礎(chǔ)類型數(shù)據(jù)(如:int,double?等)的方法。?

【示例2DataInputStreamDataOutputStream的使用

package?com.bjsxt.io;

import?java.io.*;

public?class?TestDataStream {

????public?static?void?main(String[]?args) {

??????? DataOutputStream?dos?=?null;

??????? DataInputStream?dis?=?null;

??????? FileOutputStream?fos?=?null;

??????? FileInputStream ?fis?=?null;

????????try?{

?????????????fos?=?new?FileOutputStream("D:/data.txt");

?????????????fis?=?new?FileInputStream("D:/data.txt");

?????????????dos?=?new?DataOutputStream(new?BufferedOutputStream(fos));

?????????????dis?=?new?DataInputStream(new?BufferedInputStream(fis));

?????????????//將如下數(shù)據(jù)寫入到文件中

?????????????dos.writeDouble(Math.random());

?????????????dos.writeBoolean(true);

?????????????dos.writeInt(10);

?????????????dos.writeChar('a');

?????????????dos.flush();?? ??//將流中數(shù)據(jù)寫入到文件中

?????????????//從文件中直接讀取基本數(shù)據(jù)

???????????? System.out.println("double: "?+?dis.readDouble());

???????????? System.out.println("boolean: "?+?dis.readBoolean());

???????????? System.out.println("int: "?+?dis.readInt());

???????????? System.out.println("char: "?+?dis.readChar());

??????? }?catch?(IOException?e) {

?????????????e.printStackTrace();

??????? }?finally?{

?????????????try?{

?????????????????if(dos!=null){

?????????????????????dos.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

?????????????try?{

?????????????????if(dis!=null){

?????????????????????dis.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

?????????????try?{

?????????????????if(fos!=null){

?????????????????????fos.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

?????????????try?{

?????????????????if(fis!=null){

?????????????????????fis.close();

???????????????? }

???????????? }?catch?(IOException?e) {

?????????????????e.printStackTrace();

???????????? }

??????? }

??? }

}





「全棧Java筆記」是一部能幫大家從零到一成長為全棧Java工程師系列筆記。筆者江湖人稱 Mr. G,10年Java研發(fā)經(jīng)驗,曾在神州數(shù)碼、航天院某所研發(fā)中心從事軟件設(shè)計及研發(fā)工作,從小白逐漸做到工程師、高級工程師、架構(gòu)師。精通Java平臺軟件開發(fā),精通JAVAEE,熟悉各種流行開發(fā)框架。


? 筆記包含從淺入深的六大部分:

? A-Java入門階段

? B-數(shù)據(jù)庫從入門到精通

? C-手刃移動前端和Web前端

? D-J2EE從了解到實戰(zhàn)

? E-Java高級框架精解

? F-Linux和Hadoop?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容