public static void main(String[] args) throws IOException {
//bufferedOutput();
//bufferedInput();
//copyFile();
}
/**
* 檔案複制操作(操作檔案最快)
* @throws IOException
*/
private static void copyFile() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\test.avi"));
byte[] bytes = new byte[1024];
int len = 0;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
System.out.println("複制完成");
}
/**
* BufferedInputStream:提升讀取效率,執行順序和BufferedOutputStream一樣
*/
private static void bufferedInput() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
System.out.println(new String(b, 0, len));
}
bis.close();//關閉緩沖流也會吧位元組流關閉
}
/**
* BufferedOutputStream:提升寫入效率
* 沒提升效率前順序:程式->JVM->計算機底層寫入(每次讀取*位元組,每次都調用一次計算機底層)
* 提升效率後:程式->JVM(先不調用計算機底層,先将資料緩沖,資料讀取完畢後->計算機底層)
* OutputStream的子類,使用方法都一樣
*/
private static void bufferedOutput() throws IOException{
FileOutputStream fos = new FileOutputStream("D:\\a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);//傳入要提升效率的位元組輸出流
bos.write("HelloWorld".getBytes());
bos.close();
System.out.println("寫入完成");
}