java中字節IO流BufferedInputStream和BufferOutputStream的使用

java中IO流(BufferedInputStream和BufferOutputStream拷貝)
* A:緩衝思想
    * 字節流一次讀寫一個數組的速度明顯比一次讀寫一個字節的速度快很多,
    * 這是加入了數組這樣的緩衝區效果,java本身在設計的時候,
    * 也考慮到了這樣的設計思想(裝飾設計模式後面講解),所以提供了字節緩衝區流
* B.BufferedInputStream
    * BufferedInputStream內置了一個緩衝區(數組)
    * 從BufferedInputStream中讀取一個字節時
    * BufferedInputStream會一次性從文件中讀取8192個, 存在緩衝區中, 返回給程序一個
    * 程序再次讀取時, 就不用找文件了, 直接從緩衝區中獲取
    * 直到緩衝區中所有的都被使用過, 才重新從文件中讀取8192個
* C.BufferedOutputStream
    * BufferedOutputStream也內置了一個緩衝區(數組)
    * 程序向流中寫出字節時, 不會直接寫到文件, 先寫到緩衝區中
    * 直到緩衝區寫滿, BufferedOutputStream纔會把緩衝區中的數據一次性寫到文件裏

public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("致青春.mp3");				//創建輸入流對象,關聯致青春.mp3
		FileOutputStream fos = new FileOutputStream("copy.mp3");			//創建輸出流對象,關聯copy.mp3
		BufferedInputStream bis = new BufferedInputStream(fis);				//創建緩衝區對象,對輸入流進行包裝讓其變得更加強大
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		int b;
		while((b = bis.read()) != -1) {
			bos.write(b);
		}
		
		bis.close();
		bos.close();
	}

IO流(flush和close方法的區別)
* flush()方法
    * 用來刷新緩衝區的,刷新後可以再次寫出 
* close()方法
    * 用來關閉流釋放資源的的,如果是帶緩衝區的流對象的close()方法,不但會關閉流,還會再關閉流之前刷新緩衝區,關閉後不能再寫出 

IO流(字節流讀寫中文) 
* 字節流讀取中文的問題
    * 字節流在讀中文的時候有可能會讀到半個中文,造成亂碼 
* 字節流寫出中文的問題
    * 字節流直接操作的字節,所以寫出中文必須將字符串轉換成字節數組 
    * 寫出回車換行 write("\r\n".getBytes());

public class Demo6_Chinese {

	/**
	 * @param args
	 * * 字節流讀取中文的問題
			* 字節流在讀中文的時候有可能會讀到半個中文,造成亂碼 
		* 字節流寫出中文的問題
			* 字節流直接操作的字節,所以寫出中文必須將字符串轉換成字節數組 
			* 寫出回車換行 write("\r\n".getBytes());
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		FileOutputStream fos = new FileOutputStream("zzz.txt");
		fos.write("我讀書少,你不要騙我".getBytes());
		fos.write("\r\n".getBytes());
		fos.close();
	}

###20.14_IO流(流的標準處理異常代碼1.6版本及其以前)
* try finally嵌套

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("aaa.txt");
            fos = new FileOutputStream("bbb.txt");
            int b;
            while((b = fis.read()) != -1) {
                fos.write(b);
            }
        } finally {
            try {
                if(fis != null)
                    fis.close();
            }finally {
                if(fos != null)
                    fos.close();
            }
        }

###20.15_IO流(流的標準處理異常代碼1.7版本)
* try close

        try(
            FileInputStream fis = new FileInputStream("aaa.txt");
            FileOutputStream fos = new FileOutputStream("bbb.txt");
            MyClose mc = new MyClose();
        ){
            int b;
            while((b = fis.read()) != -1) {
                fos.write(b);
            }
        }
* 原理
    * 在try()中創建的流對象必須實現了AutoCloseable這個接口,如果實現了,在try後面的{}(讀寫代碼)執行後就會自動調用,流對象的close方法將流關掉 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo7_TryFinally {

	/**
	 * @param args
	 * @throws IOException 
      1.7版本就可以用這種寫法
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		try(
			FileInputStream fis = new FileInputStream("xxx.txt");
			FileOutputStream fos = new FileOutputStream("yyy.txt");
		){
			int b;
			while((b = fis.read()) != -1) {
				fos.write(b);
			}
		}
	}

/**IO流的異常標準處理代碼**/
	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("xxx.txt");
			fos = new FileOutputStream("yyy.txt");
			
			int b;
			while((b = fis.read()) != -1) {
				fos.write(b);
			}
		}finally {
			try{
				if(fis != null)
					fis.close();
			}finally {							//try fianlly的嵌套目的是能關一個儘量關一個
				if(fos != null)
					fos.close();
			}
		}
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章