標籤:string 方法 exce int 字元 ext3 字元流 out public
位元組緩衝流
//為什麼要使用封裝流,使用封裝流是為了提高讀寫操作的效能。public class Packing_flowDemo {public static void main(String[] args) throws Exception {File file = new File("file/packing_flow.txt");//封裝流的寫法,緩衝區記憶體大小。1024*8=8192 (byte)//BufferedOutputStream packing = new BufferedOutputStream(new FileOutputStream(file, true));//packing.write("大家好!你好嗎?how are your !".getBytes());//packing.close(); //封裝流的讀寫操作。BufferedInputStream outPacking = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[1024];int len = -1;while ((len = outPacking.read(buffer)) != -1) {System.out.println(new String(buffer, 0, len));}}}
public static void main(String[] args) throws IOException {//為了代碼看起來美觀一些,直接拋出去File file=new File("moves/許嵩 - 素顏 - 現場版.mp3");File file1=new File("moves/許嵩 - 素顏.mp3");//text(file, file1);//text2(file, file1);//text3(file, file1);text4(file, file1);}private static void text(File file,File file1) throws IOException {//節點流的方法,一個一個位元組的讀和寫long begin=System.currentTimeMillis();FileInputStream in=new FileInputStream(file);FileOutputStream out =new FileOutputStream(file1);int len=-1;while((len=in.read())!=-1){out.write(len);}in.close();out.close();System.out.println(System.currentTimeMillis()-begin);//5547毫秒}private static void text2(File file,File file1) throws IOException {//緩衝流的寫法,一個一個位元組的讀和寫long begin=System.currentTimeMillis();BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1));int len=-1;while(in.read()!=-1){out.write(len);}in.close();out.close();System.out.println(System.currentTimeMillis()-begin);//63毫秒}private static void text3(File file,File file1) throws IOException {//節點流的寫法,一次性讀取1024個位元組long begin=System.currentTimeMillis();FileInputStream in=new FileInputStream(file);FileOutputStream out =new FileOutputStream(file1);int len=-1;byte[] buffer=new byte[1024];while((len=in.read(buffer))!=-1){out.write(buffer,0,len);}in.close();out.close();System.out.println(System.currentTimeMillis()-begin);//38毫秒}private static void text4(File file,File file1) throws IOException {//緩衝流的寫法,一次性讀取1024個位元組long begin=System.currentTimeMillis();BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1));int len=-1;byte[] buffer=new byte[1024];while((len=in.read(buffer))!=-1){out.write(buffer,0,len);}in.close();out.close();System.out.println(System.currentTimeMillis()-begin);//4毫秒}
Java IO輸入輸出資料流 FileWriter 字元流