標籤:不同 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輸入/輸出流的套接