1. 程式人生 > >【原】Java學習筆記033

【原】Java學習筆記033

  1 package cn.temptation;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 
  9 public class Sample27 {
 10     public static void main(String[] args) throws
IOException { 11 // 比較IO操作的耗時 12 long startTime = System.currentTimeMillis(); 13 14 // method1("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3"); 15 // method2("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3"); 16 // method3("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3");
17 method4("D:\\KuGou\\田馥甄 - 傻子.mp3", "D:\\田馥甄 - 傻子.mp3"); 18 19 long endTime = System.currentTimeMillis(); 20 21 System.out.println("耗時:" + (endTime - startTime)); 22 23 // 顯然,使用緩衝區比不使用緩衝區耗時少了很多 24 // 類比理解:古代歐洲人寫字,拿著棍子一次蘸一滴墨;古代中國人寫字,拿著毛筆一次蘸一管墨(容器思想)
25 } 26 27 /** 28 * 位元組流一次讀寫一個位元組(耗時:38080) 29 * @param src 30 * @param target 31 * @throws IOException 32 */ 33 public static void method1(String src, String target) throws IOException { 34 FileInputStream fis = new FileInputStream(src); 35 FileOutputStream fos = new FileOutputStream(target); 36 37 int ch = 0; 38 39 while ((ch = fis.read()) != -1) { 40 fos.write(ch); 41 fos.flush(); 42 } 43 44 fos.close(); 45 fis.close(); 46 } 47 48 /** 49 * 位元組流一次讀寫一個位元組陣列(位元組陣列作為緩衝區)(耗時:78) 50 * @param src 51 * @param target 52 * @throws IOException 53 */ 54 public static void method2(String src, String target) throws IOException { 55 FileInputStream fis = new FileInputStream(src); 56 FileOutputStream fos = new FileOutputStream(target); 57 58 byte[] arr = new byte[1024]; 59 int length = 0; 60 61 while ((length = fis.read(arr)) != -1) { 62 fos.write(arr, 0, length); 63 fos.flush(); 64 } 65 66 fos.close(); 67 fis.close(); 68 } 69 70 /** 71 * 緩衝位元組流一次讀寫一個位元組(耗時:23922) 72 * @param src 73 * @param target 74 * @throws IOException 75 */ 76 public static void method3(String src, String target) throws IOException { 77 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); 78 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); 79 80 int ch = 0; 81 82 while ((ch = bis.read()) != -1) { 83 bos.write(ch); 84 bos.flush(); 85 } 86 87 bos.close(); 88 bis.close(); 89 } 90 91 /** 92 * 緩衝位元組流一次讀寫一個位元組陣列(位元組陣列作為緩衝區)(耗時:78) 93 * @param src 94 * @param target 95 * @throws IOException 96 */ 97 public static void method4(String src, String target) throws IOException { 98 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); 99 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); 100 101 byte[] arr = new byte[1024]; 102 int length = 0; 103 104 while ((length = bis.read(arr)) != -1) { 105 bos.write(arr, 0, length); 106 bos.flush(); 107 } 108 109 bos.close(); 110 bis.close(); 111 } 112 }

相關推薦

Java學習筆記033

1 package cn.temptation; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream;

Java學習筆記034 - 網絡

端口號 bre throws clas 編程 綁定 容器 名稱 套接字 1 package cn.temptation; 2 3 import java.net.InetAddress; 4 5 public class Sample01 {

Java學習筆記034

1 package cn.temptation; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.ne

Java學習筆記032

1 package cn.temptation; 2 3 public class Sample03 { 4 public static void main(String[] args) { 5 /* 6 * Thread類的常用成員方法:

javascript學習筆記之this用法

javascript中的this學習起來相對複雜,最近花了點時間研究,總結起來大概這隻有5種情況,相信只要熟悉這5種用法,基本是可以解決所有的this問題,文字不介紹this設計原理,只介紹用法,閱讀本文,你需要了解javascript執行上下文環境,博主寫這種文章的目的,主要還是給自己做下筆記,後續也會輸出

專欄 - Java學習筆記經典例題

Java學習筆記經典例題 在校期間,記錄在一家培訓機構學習Android期間的JAVA筆記經典例題部落格專欄。歡迎大家互相交流學習。

JavaJava學習筆記總結(一)

2013-07-15 1. JDK、JRE和JVM分別是什麼,區別是什麼? 答: ①、JDK 是整個Java的核心,包括了Java執行環境、Java工具和Java基礎類庫。 ②、JRE(Java Runtime Environment,Java執行環境),執行JAVA程式所

Nodejs學習筆記(一)--- 簡介及安裝Node.js開發環境

ack 目錄 javascrip 難度 時間 網站開發 clas jetbrains 常用 目錄 學習資料 簡介 安裝Node.js npm簡介 開發工具 Sublime Node.js開發環境配置 擴展:安裝多版本管理器 學習資料   1.深入淺出Node.j

pythonpython學習筆記

pythonList:列表1)列表表達式:有時可以很方便的代替函數實現一些功能1.1)字符串居中(長度為9個字符) >>> a = ["123","456","abc","Abc","AAA"] >>> [k.center(9) for k in a] [‘ 123

Python3學習筆記(urllib模塊的使用)

nal 方法 utf 網址 pin des IE tps erer 原文地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None, [ti

MongoDB學習筆記(查詢)

順序 god ... ive HR 操作 方式 mar obj 原文地址 MongoDB學習筆記(查詢) 基本查詢: 構造查詢數據。 > db.test.findOne() { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a")

轉載JAVA學習路線二

get rocket 原理 aop 二次 三次握手 active AD cpu JAVA學習路線二------------高階面試 作者:Java高級進階鏈接:https://zhuanlan.zhihu.com/p/35301291來源:知乎著作權歸作者所有。商業轉載請聯

Verilog學習筆記簡單功能實現(八)...............異步FIFO

另一個 gif 多個 可靠 基本原理 drs bar next 不同 基本原理: 1.讀寫指針的工作原理   寫指針:總是指向下一個將要被寫入的單元,復位時,指向第1個單元(編號為0)。   讀指針:總是指向當前要被讀出的數據,復位時,指向第1個單元(編號為0)

Java學習---內存溢出的排查經歷

heap cspro 大量 每次 並且 老年代 操作 常見 lvm 【原文】https://www.toutiao.com/i6595365358301872643/ 前言 OutOfMemoryError 問題相信很多朋友都遇到過,相對於常見的業務異常(數組越界、空指針等

Java學習---線程間的通信

更強 裏的 兩個 sub 優先 網絡 sync 獲取 操作系統 【原文】https://www.toutiao.com/i6572378564534993415/ 兩個線程間的通信 這是我們之前的線程。 執行效果:誰搶到資源,誰運行~ 實現線程交替執行: 這裏主要用到

Java學習---Java的鎖和Mysql的鎖機制

tps www. 鎖機制 www http ava mysql href 和數 【原文】https://www.toutiao.com/i6593861446428262916/ Java和數據庫的鎖機制 https://www.toutiao.com/i659386144

Java學習---HashMap和HashSet的內部工作機制

link 實踐 離散 val 數據結構 結構 通過 如何 factor 【原文】https://www.toutiao.com/i6593863882484220430/ HashMap和HashSet的內部工作機制 HashMap 和 HashSet 內部是如何工作的?散

Java學習---算法那些事

tco 那些事 www com 遊學 leetcode 更多 ava 今日頭條 【更多參考】 LeetCode算法 每日一題 1: Two Sum ----> 更多參考【今日頭條--松鼠遊學】 史上最全的五大算法總結【轉】Java學習---算法那些事

Java學習---Java核心數據結構(List,Map,Set)使用技巧與優化

系統資源 .get 兩種 這樣的 his java學習 com 都是 索引 【原文】https://www.toutiao.com/i6594587397101453827/ Java核心數據結構(List,Map,Set)使用技巧與優化 JDK提供了一組主要的數據結構實現

Java學習---快速掌握RPC原理及實現

消費者 阿裏 局限 kryo nes 很多 cal 網絡 href 【原文】https://www.toutiao.com/i6592365493435236872/ ?RPC概述 RPC(Remote Procedure Call)即遠程過程調用,也就是說兩臺服務器A,