午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

java 實(shí)現(xiàn)字節(jié)流和字節(jié)緩沖流讀寫文件時間對比

瀏覽:200日期:2022-08-18 09:06:20

我就廢話不多說了,大家還是直接看代碼吧~

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個方式 * 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);}/* * 方法,實(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ū)流讀寫單個字節(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é)流讀寫單個字節(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 { // 獲取開始時間 long start = System.currentTimeMillis(); // 1. 創(chuàng)建一個文件字節(jié)輸入流對象, 關(guān)聯(lián)源文件 InputStream in = new FileInputStream('C:UsersJacktemp柳巖.jpg'); // 2. 創(chuàng)建一個文件字節(jié)輸出流對象, 關(guān)聯(lián)目標(biāo)文件 File file = new File('C:UsersJackmyDocly.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é)大小的內(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é)束時間 long end = System.currentTimeMillis(); System.out.println('毫秒: ' + (end - start)); }}

注:

File file = new File('C:UsersJackmyDocly.jpg');

new File(文件路徑名稱),方法里面如果只寫了文件名。格式,這是絕對路徑,位置在當(dāng)前的工作空間里面。

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 免费在线观看黄色小视频 | 一区二区黄色 | 欧美巨大另类极品videosbest | 97免费在线观看视频 | 一级α片 | 欧美三级在线播放 | 国产一级片a | 亚洲综合在线视频 | av爱爱| 毛片视频在线免费观看 | 日韩美女网站 | 黄色在线免费播放 | 亚洲精品91 | 国产国语性生话播放 | 天天干妹子 | 在线观看免费视频黄 | 国产一级片免费在线观看 | 久久久久免费视频 | 亚洲自拍色 | 亚洲黄色一级大片 | 日韩一区二区三区在线观看 | 国产精品久久久久久在线观看 | 国产一级在线播放 | 国产伦精品一区二区三区免.费 | 日本欧美一区二区三区不卡视频 | 久久精品亚洲 | 亚洲一区二三区 | 婷婷成人av | 国产精品久久久亚洲 | 懂色av成人一区二区三区 | 日本天天色 | 日韩国产在线 | 亚洲女优av| 91精品网 | 中文字幕在线2018 | 亚洲四虎影院 | 亚洲精品9 | 51av视频 | 欧洲亚洲一区二区 | 久色资源 | 最新天堂av |