国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java---IO流緩沖輸入輸出流

2019-11-06 07:46:19
字體:
來源:轉載
供稿:網友

這里寫圖片描述

☆示例測試技術總結:方案1是最優的 1)有buffer比沒有更快; 2)buffer放在中間層包裝比放在外層更快; 3)按行或按塊操作 比 按字節或字符操作更快(用Object流操作的速度 比 字節字符方式 更快) 4)緩沖區要結合流才可以使用,在流的基礎上對流的功能進行了增強。


package cn.hncu.io.buffered;import java.io.BufferedInputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class BufferedXXXStream { public static void main(String[] args) { // 只要演示使用buffered緩沖流和不使用緩沖流(而且緩沖流所在的位置)讀取文件的速度進行比較 // bufferedRead1();//24 25 26左右的樣子 // bufferedRead2();//80~90毫秒之間的樣子 bufferedRead3();//27以上的樣子 } PRivate static void bufferedRead3() { // 測試程序運行的時長 long time1 = System.currentTimeMillis(); File file = new File("files/buffered.txt"); if (!file.exists()) { System.out.println("文件沒有找到...."); return; } byte[] buf = new byte[50]; // 方式3:buffered緩沖流放在最外層進行包裝 try { BufferedInputStream bis = new BufferedInputStream( new DataInputStream(new FileInputStream(file))); int len = 0; while ((len = bis.read(buf)) != -1) { System.out.println(new String(buf, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } long time2 = System.currentTimeMillis(); System.out.println("程序運行的時長為(毫秒):" + (time2 - time1)); } private static void bufferedRead2() { // 測試程序運行的時長 long time1 = System.currentTimeMillis(); File file = new File("files/buffered.txt"); if (!file.exists()) { System.out.println("文件沒有找到...."); return; } // 方式2:不使用buffered緩沖流 try { DataInputStream dis = new DataInputStream(new FileInputStream(file)); String line = ""; while ((line = dis.readLine()) != null) { System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } long time2 = System.currentTimeMillis(); System.out.println("程序運行的時長為(毫秒):" + (time2 - time1)); } private static void bufferedRead1() { // 測試程序運行的時長 long time1 = System.currentTimeMillis(); File file = new File("files/buffered.txt"); if (!file.exists()) { System.out.println("文件沒有找到...."); return; } // 方式1:將buffered緩沖流放在中間位置 try { DataInputStream dis = new DataInputStream(new BufferedInputStream( new FileInputStream(file))); String line = ""; while ((line = dis.readLine()) != null) { System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } long time2 = System.currentTimeMillis(); System.out.println("程序運行的時長為(毫秒):" + (time2 - time1)); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 综艺| 伊宁县| 容城县| 同仁县| 定日县| 太湖县| 楚雄市| 定陶县| 江门市| 翼城县| 文水县| 延边| 东兰县| 文成县| 贵阳市| 南平市| 沙田区| 当涂县| 溧水县| 安龙县| 绥阳县| 梅州市| 鹰潭市| 龙州县| 丽水市| 健康| 崇信县| 弥勒县| 古交市| 隆昌县| 新河县| 开封市| 会宁县| 姚安县| 双桥区| 沙田区| 平度市| 长治县| 鲁甸县| 莆田市| 河曲县|