Java IO輸入輸出資料流 FileWriter 字元流

來源:互聯網
上載者:User

標籤: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 字元流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.