【IO流】16 - 字節流 - 自定義緩沖數組復制文件
阿新 • • 發佈:2018-02-01
color inpu 關聯 cas 緩沖區 相關 數組 輸入 des
package cn.itcast.io.c.bytestream.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileByBufferTest { /** * @param args * @throws IOException */ publicstatic void main(String[] args) throws IOException { File srcFile = new File("E:\\1.mp3"); File destFile = new File("E:\\copy_1.mp3"); // 2,明確字節流 輸入流和源相關聯,輸出流和目的關聯。 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile);// 3,定義一個緩沖區。 byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len);// 將數組中的指定長度的數據寫入到輸出流中。 } // 4,關閉資源。 fos.close(); fis.close(); } }
【IO流】16 - 字節流 - 自定義緩沖數組復制文件