上節(jié)我們講到「Java中常用流:文件字節(jié)流和字節(jié)數(shù)組流」肘习,本節(jié)我們繼續(xù)走起~
?緩沖流
BufferedInputStream和BufferedOutputStream這兩個流是處理流盒卸,通過內(nèi)部緩存數(shù)組來提高操作流的效率。
【示例1】使用緩沖流實現(xiàn)文件的高效率復制操作
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?等)的方法。?
【示例2】DataInputStream和DataOutputStream的使用
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?