java 實現(xiàn)字節(jié)流和字節(jié)緩沖流讀寫文件時間對比
更新時間:2021年01月20日 15:48:58 作者:dxm809
這篇文章主要介紹了java 實現(xiàn)字節(jié)流和字節(jié)緩沖流讀寫文件時間對比,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,大家還是直接看代碼吧~
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;
/*
* 文件復制方式,字節(jié)流,一共4個方式
* 1. 字節(jié)流讀寫單個字節(jié) 125250 毫秒
* 2. 字節(jié)流讀寫字節(jié)數(shù)組 193 毫秒 OK
* 3. 字節(jié)流緩沖區(qū)流讀寫單個字節(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);
}
/*
* 方法,實現(xiàn)文件復制
* 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();
}
/*
* 方法,實現(xiàn)文件復制
* 3. 字節(jié)流緩沖區(qū)流讀寫單個字節(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();
}
/*
* 方法,實現(xiàn)文件復制
* 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();
}
/*
* 方法,實現(xiàn)文件復制
* 1. 字節(jié)流讀寫單個字節(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();
}
}
補充:輸入流輸出流快速讀寫方式
這是以前整理的,今天看到了,就放到博客中!
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 {
// 獲取開始時間
long start = System.currentTimeMillis();
// 1. 創(chuàng)建一個文件字節(jié)輸入流對象, 關聯(lián)源文件
InputStream in = new FileInputStream("C:\\Users\\Jack\\temp\\柳巖.jpg");
// 2. 創(chuàng)建一個文件字節(jié)輸出流對象, 關聯(lián)目標文件
File file = new File("C:\\Users\\Jack\\myDoc\\ly.jpg");
if (!file.exists()) {
// 如果文件不存在, 就需要創(chuàng)建
File parentFile = file.getParentFile();
parentFile.mkdirs();
}
OutputStream out = new FileOutputStream(file);
// 3. 讀取與寫入
byte[] buf = new byte[1024]; //分配1024個字節(jié)大小的內存給buf
int len = -1;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
// 4. 關閉資源
out.close();
in.close();
// 獲取結束時間
long end = System.currentTimeMillis();
System.out.println("毫秒: " + (end - start));
}
}
注:
File file = new File("C:\Users\Jack\myDoc\ly.jpg");
new File(文件路徑名稱),方法里面如果只寫了文件名。格式,這是絕對路徑,位置在當前的工作空間里面。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Java基于JDBC連接數(shù)據(jù)庫及顯示數(shù)據(jù)操作示例
這篇文章主要介紹了Java基于JDBC連接數(shù)據(jù)庫及顯示數(shù)據(jù)操作,結合實例形式分析了Java使用jdbc進行mysql數(shù)據(jù)庫連接與數(shù)據(jù)讀取、顯示等相關操作技巧,需要的朋友可以參考下2018-06-06
使用Maven將springboot工程打包成docker鏡像
這篇文章主要介紹了使用Maven將springboot工程打包成docker鏡像,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
java使用CountDownLatch等待多線程全部執(zhí)行完成
這篇文章主要為大家詳細介紹了使用CountDownLatch等待多線程全部執(zhí)行完成,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
Java使用@Autowired注解獲取對象為null的幾種情況及解決方法
這篇文章主要給大家介紹了使用@Autowired注解獲取對象為null的幾種情況以及?解決方法,文中有詳細的代碼示例講解,具有一定的參考價值,需要的朋友可以參考下2023-09-09

