Java IO輸入/輸出流的套接

來源:互聯網
上載者:User

標籤:不同   leo   span   file   利用   inux   情境   rtti   ati   

緩衝字元流BufferedInputStream / BufferedOutputStream

檔案操作流 InputStream / OutputStream

java 輸入輸出資料流的套接,測試一下流鏈的速度       

因為節點流在程式中並不是很常用,一般常通過過濾流將多個流套接在一起,利用各流的特性共同處理資料,套接的多個流構成了一個流鏈;

我們來分析他們的已耗用時間。(因為在不同的系統(maxOX,Linux,WIN(32/64))上啟動並執行時間可能都不相同)我截取了幾段時間來進行對比。

1.緩衝字元流BufferedInputStream / BufferedOutputStream

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Main {    public static void main(String[] args) throws IOException {        long StartTime = System.nanoTime();        File address = new File("D:"+File.separator+"c.txt");        File address1 = new File("D:"+File.separator+"ww.txt");                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(address));        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(address1));        int c ;        while((c=bin.read())!=-1) {            bout.write(c);        }        bin.close();        bout.flush();        bout.close();        System.out.println("複製完成");        long endTime = System.nanoTime();        System.out.println((endTime-StartTime)+"ms");    }}

 

 

 2.檔案操作流 InputStream / OutputStream

 

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class Main {    public static void main(String[] args)throws IOException {        long StartTime = System.nanoTime();        File address = new File("D:"+File.separator+"c.txt");        File address1 = new File("D:"+File.separator+"ww.txt");                InputStream in = new FileInputStream(address);        OutputStream out = new FileOutputStream (address1);                int c ;        while((c=in.read())!=-1) {            out.write(c);        }        in.close();        out.flush();        out.close();        System.out.println("複製完成");        long endTime = System.nanoTime();        System.out.println((endTime-StartTime)+"ms");    }}

 

3.java 輸入輸出資料流的套接

 

 

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class Main {    public static void main(String[] args) throws IOException {        long StartTime = System.nanoTime();        File address = new File("D:"+File.separator+"c.txt");        File address1 = new File("D:"+File.separator+"ww.txt");                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(address));        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(address1));                InputStream din = new DataInputStream(bin);        OutputStream dout = new DataOutputStream(bout);        int c ;        while((c = din.read())!=-1) {            dout.write(c);        }                bin.close();        bout.flush();        bout.close();                din.close();        dout.flush();        dout.close();                System.out.println("複製成功");        long endTime = System.nanoTime();        System.out.println((endTime-StartTime)+"ms");        }}

 

 

他們的已耗用時間在某種程度上是類似的,因為他們每次唯讀取一個字元,然後寫入到檔案中。

他們的使用需要看情境選擇:

普通的檔案輸入輸出時 選擇 FileInputStream類

在需要更快的輸入輸出時選擇 Buffering類 

在多重輸入輸出時選擇Java 輸入輸出資料流的套接,可以方便處理資料並提高處理的效率。

 

Java IO輸入/輸出流的套接

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.