java基礎專欄—IOBuffer(2)

緩衝概述

​ 在讀寫的操做中,讀寫操做的效率是十分重要的,sun公司提供了一套相應的類來提高效率。java

​ ==IO對象==沒有具體的操做,它是找操做系統來實現這個操做,因此每一個字節都去找操做系統,就會很慢,buffer就是先寫入緩衝區,等寫到必定的數量在一次性找操做系統寫入文件。數組

BufferedOutputStream

​ 該類實現了緩衝的輸出流,經過設置這種輸出流,應用程序就能夠將各個字節寫入底層的輸出流中,而沒必要針對每一個字節調底層輸出流網絡

|--OutputStream
	|--BufferedOutputStream
public BufferedOutputStream(OutputStream out)
  
public BufferedOutputStream(OutputStream out, int size)
  
public void write(byte[] b, int off, int len)
  
public void write(int b)將指定的字節寫入此緩衝的輸出流

public void flush()
FileOutputStream fos = new FileOutputStream("xxxx");

BufferedOutputStream bos = new BufferedOutputStream(fos);

bos.write("xxx");

byte[] bytes = "xxxx".getBytes();

bos.write(byte);

bos.close();

BufferedInputStream

咱們一直使用流對象就BufferedInputStream,打印流printStream操作系統

|--InputStream
	|--BufferedInputStream
public BufferedInputStream(InputStream in)

public BufferedInputStream(InputStream in, int size)
  
public void read(byte[] b);

public void flush();
FileInputStream

四種複製方法的效率比較

字節流讀取單個字節code

FileInputStream fis = null;
FileOutputStream fos = null;
int len = 0;

try{
    fis = new FileInputStream("xxx");
  	fos = new FileOutputStream("xxx");
  	while((len = fis.read()) != -1){
        fos.write((char)len);
    }
}catch(IOException ex){
    System.out.println(ex.getMessage("讀寫失敗"));
}finally{
    if((fis != null) || (fos != null)){
     	try{
        	fis.close()
            fos.close()
    	}catch(IOException ex){
        	System.out.println(ex.getMessage("關閉流失敗"));
    	}   
    }
}

字節流讀取字符數組對象

FileInputStream fis = null;
FileOutputStream fos = null;
byte[] bytes = new byte[1024];
int len = 0;

try{
    fis = new FileInputStream("xxx");
  	fos = new FileOutputStream("xxx");
  	while((len = fis.read(bytes)) != -1){
        fos.write(bytes, 0 ,len);
    }
}catch(IOException ex){
    System.out.println(ex.getMessage("讀寫失敗"));
}finally{
    if((fis != null) || (fos != null)){
     	try{
        	fis.close()
            fos.close()
    	}catch(IOException ex){
        	System.out.println(ex.getMessage("關閉流失敗"));
    	}   
    }
}

字節流緩衝區讀取單個字節內存

FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int len = 0;

try{
    fis = new FileInputStream("xxx");
  	fos = new FileOutputStream("xxx");
	bis = new BufferedInputStream(fis);
	bos = new BufferedOutputStream(fos);
  	while((len = bis.read()) != -1){
        bos.write((char)len);
    }
}catch(IOException ex){
    System.out.println(ex.getMessage("讀寫失敗"));
}finally{
    if((bis != null) || (bos != null)){
     	try{
        	bis.close()
            bos.close()
    	}catch(IOException ex){
        	System.out.println(ex.getMessage("關閉流失敗"));
    	}   
    }
}

字節流緩衝區讀取字符數組get

FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int len = 0;
byte[] bytes = new byte[1024];

try{
    fis = new FileInputStream("xxx");
  	fos = new FileOutputStream("xxx");
	bis = new BufferedInputStream(fis);
	bos = new BufferedOutputStream(fos);
  	while((len = bis.read(bytes)) != -1){
        bos.write(bytes, 0 ,len);
    }
}catch(IOException ex){
    System.out.println(ex.getMessage("讀寫失敗"));
}finally{
    if((bis != null) || (bos != null)){
     	try{
        	bis.close()
            bos.close()
    	}catch(IOException ex){
        	System.out.println(ex.getMessage("關閉流失敗"));
    	}   
    }
}

BufferedWriter

|--Writer
	|--OutputStreamWriter
	|--BufferedWriter
	
public BufferedWriter(Writer writer)
  
public void newLine()

BufferedReader

|--Reader
	|--InputStreamReader
	|--BufferedReader
	
public BufferedReader(Reader reader)

public String readLine()

IO流對象的使用步驟

  1. 明確操做的是數據源還有數據目的
    1. 源,InputStream ,Reader
    2. 目的, OutputStrea, Writer
  2. 要操做的是文本仍是字節
  3. 明確數據所在的設備
    1. 硬盤,文件File對象
    2. 內存,數組,String
    3. 鍵盤,System.in,System.out
    4. 網絡,Socker
  4. 是否須要額外的功能
    1. 轉換嗎?轉換流InputStreamReader,OutputStreamWriter
    2. 高效嗎?BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter
相關文章
相關標籤/搜索