對接輸入輸出流 封裝拷貝 封裝釋放資源

2021-09-12 01:48:42 字數 1234 閱讀 2771

/*p14

* 封裝拷貝

* 封裝釋放資源

*/public class fileutils catch (ioexception e)

//檔案到位元組陣列

byte datas = null;

try catch (filenotfoundexception e)

//位元組陣列到檔案

try catch (filenotfoundexception e) }/*

* 對接輸入輸出流1  自寫釋放資源

*/public static void copy(inputstream is, outputstream os)

os.flush();

} catch (filenotfoundexception e) catch (ioexception e) finally

}/**

*      * 對接輸入輸出流2 jdk try()...catch...resource 新jdk1.8方法

*  try(inputstream is = new fileinputstream("abc.txt");

outputstream os = new fileoutputstream("abc-copy.txt");)catch()

finally省略

* @param is

* @param os

*/public static void copy1(inputstream is, outputstream os)

os.flush();

} catch (filenotfoundexception e) catch (ioexception e) */

}/**

* 釋放資源

* @param is

* @param os

*/public static void close(inputstream is, outputstream os)

} catch (ioexception e)     

try

} catch (ioexception e)

}/**

* 釋放資源 :關閉實現了closeable介面的流

* @param ios

*/public static void colse(closeable... ios)    

} catch (ioexception e)

}    }}

檔案輸入輸出流與位元組陣列輸入輸出流的對接

過程 1 檔案 程式 位元組陣列 2 位元組陣列 程式 檔案 方式一 分開寫,比較簡單 思路 過程一 將檔案中的內容不斷通過程式 使用fileinputstream讀取檔案,使用bytearrayoutputstream寫入到緩衝陣列 讀到緩衝陣列中,過程二 將緩衝陣列的內容不斷地通過程式 使用by...

輸入輸出流

c 通過以下幾個類支援檔案的輸入和輸出 ofstream寫操作的檔案類由ostream引申而來 ifstream讀操作的檔案類由istream引申而來 fstream可同時讀寫操作的檔案類由iostream引申而來 ifstream in tian.txt 開啟乙個檔案 ifstream in in...

輸入輸出流

預定義流類的物件與通用的流運算子 1 cin 2 cout 3 cerr是ostream類物件,在標準輸出裝置上顯示錯誤資訊 不帶緩衝,立即顯示 輸入輸出流 ostream 和 ofstream istream 和 ifstream fstream 定義檔案輸出流物件 fstream outfile...