在讀寫的操做中,讀寫操做的效率是十分重要的,sun公司提供了一套相應的類來提高效率。java
==IO對象==沒有具體的操做,它是找操做系統來實現這個操做,因此每一個字節都去找操做系統,就會很慢,buffer就是先寫入緩衝區,等寫到必定的數量在一次性找操做系統寫入文件。數組
該類實現了緩衝的輸出流,經過設置這種輸出流,應用程序就能夠將各個字節寫入底層的輸出流中,而沒必要針對每一個字節調底層輸出流
。網絡
|--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,打印流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("關閉流失敗")); } } }
|--Writer |--OutputStreamWriter |--BufferedWriter public BufferedWriter(Writer writer) public void newLine()
|--Reader |--InputStreamReader |--BufferedReader public BufferedReader(Reader reader) public String readLine()