java 實(shí)現(xiàn)字節(jié)流和字節(jié)緩沖流讀寫文件時(shí)間對(duì)比
我就廢話不多說了,大家還是直接看代碼吧~
package cn.itcast.copy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 文件復(fù)制方式,字節(jié)流,一共4個(gè)方式 * 1. 字節(jié)流讀寫單個(gè)字節(jié) 125250 毫秒 * 2. 字節(jié)流讀寫字節(jié)數(shù)組 193 毫秒 OK * 3. 字節(jié)流緩沖區(qū)流讀寫單個(gè)字節(jié) 1210 毫秒 * 4. 字節(jié)流緩沖區(qū)流讀寫字節(jié)數(shù)組 73 毫秒 OK */ public class Copy { public static void main(String[] args)throws IOException { long s = System.currentTimeMillis(); copy_4(new File("c:\\q.exe"), new File("d:\\q.exe")); long e = System.currentTimeMillis(); System.out.println(e-s); } /* * 方法,實(shí)現(xiàn)文件復(fù)制 * 4. 字節(jié)流緩沖區(qū)流讀寫字節(jié)數(shù)組 */ public static void copy_4(File src,File desc)throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc)); int len = 0 ; byte[] bytes = new byte[1024]; while((len = bis.read(bytes))!=-1){ bos.write(bytes,0,len); } bos.close(); bis.close(); } /* * 方法,實(shí)現(xiàn)文件復(fù)制 * 3. 字節(jié)流緩沖區(qū)流讀寫單個(gè)字節(jié) */ public static void copy_3(File src,File desc)throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc)); int len = 0 ; while((len = bis.read())!=-1){ bos.write(len); } bos.close(); bis.close(); } /* * 方法,實(shí)現(xiàn)文件復(fù)制 * 2. 字節(jié)流讀寫字節(jié)數(shù)組 */ public static void copy_2(File src,File desc)throws IOException{ FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(desc); int len = 0 ; byte[] bytes = new byte[1024]; while((len = fis.read(bytes))!=-1){ fos.write(bytes,0,len); } fos.close(); fis.close(); } /* * 方法,實(shí)現(xiàn)文件復(fù)制 * 1. 字節(jié)流讀寫單個(gè)字節(jié) */ public static void copy_1(File src,File desc)throws IOException{ FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(desc); int len = 0 ; while((len = fis.read())!=-1){ fos.write(len); } fos.close(); fis.close(); } }
補(bǔ)充:輸入流輸出流快速讀寫方式
這是以前整理的,今天看到了,就放到博客中!
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 Demo { public static void main(String[] args) throws IOException { // 獲取開始時(shí)間 long start = System.currentTimeMillis(); // 1. 創(chuàng)建一個(gè)文件字節(jié)輸入流對(duì)象, 關(guān)聯(lián)源文件 InputStream in = new FileInputStream("C:\\Users\\Jack\\temp\\柳巖.jpg"); // 2. 創(chuàng)建一個(gè)文件字節(jié)輸出流對(duì)象, 關(guān)聯(lián)目標(biāo)文件 File file = new File("C:\\Users\\Jack\\myDoc\\ly.jpg"); if (!file.exists()) { // 如果文件不存在, 就需要?jiǎng)?chuàng)建 File parentFile = file.getParentFile(); parentFile.mkdirs(); } OutputStream out = new FileOutputStream(file); // 3. 讀取與寫入 byte[] buf = new byte[1024]; //分配1024個(gè)字節(jié)大小的內(nèi)存給buf int len = -1; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } // 4. 關(guān)閉資源 out.close(); in.close(); // 獲取結(jié)束時(shí)間 long end = System.currentTimeMillis(); System.out.println("毫秒: " + (end - start)); } }
注:
File file = new File("C:\Users\Jack\myDoc\ly.jpg");
new File(文件路徑名稱),方法里面如果只寫了文件名。格式,這是絕對(duì)路徑,位置在當(dāng)前的工作空間里面。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
IntelliJ IDEA(2019)之mybatis反向生成的實(shí)現(xiàn)
這篇文章主要介紹了IntelliJ IDEA(2019)之mybatis反向生成,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負(fù)數(shù)的解決
本文主要介紹了Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負(fù)數(shù)的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Java的MyBatis+Spring框架中使用數(shù)據(jù)訪問對(duì)象DAO模式的方法
Data Access Object數(shù)據(jù)訪問對(duì)象模式在Java操作數(shù)據(jù)庫部分的程序設(shè)計(jì)中經(jīng)常被使用到,這里我們就來看一下Java的MyBatis+Spring框架中使用數(shù)據(jù)訪問對(duì)象DAO模式的方法:2016-06-06Spring ApplicationListener源碼解析
這篇文章主要為大家介紹了Spring ApplicationListener源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Java利用移位運(yùn)算將int型分解成四個(gè)byte型的方法
今天小編就為大家分享一篇關(guān)于Java利用移位運(yùn)算將int型分解成四個(gè)byte型的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12linux的shell命令檢測(cè)某個(gè)java程序是否執(zhí)行
ps -ef |grep java|grep2016-04-04