av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

java 實現字節流和字節緩沖流讀寫文件時間對比

瀏覽:93日期: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; /* * 文件復制方式,字節流,一共4個方式 * 1. 字節流讀寫單個字節 125250 毫秒 * 2. 字節流讀寫字節數組 193 毫秒 OK * 3. 字節流緩沖區流讀寫單個字節 1210 毫秒 * 4. 字節流緩沖區流讀寫字節數組 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);}/* * 方法,實現文件復制 * 4. 字節流緩沖區流讀寫字節數組 */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();}/* * 方法,實現文件復制 * 3. 字節流緩沖區流讀寫單個字節 */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();}/* * 方法,實現文件復制 * 2. 字節流讀寫字節數組 */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();}/* * 方法,實現文件復制 * 1. 字節流讀寫單個字節 */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. 創建一個文件字節輸入流對象, 關聯源文件 InputStream in = new FileInputStream('C:UsersJacktemp柳巖.jpg'); // 2. 創建一個文件字節輸出流對象, 關聯目標文件 File file = new File('C:UsersJackmyDocly.jpg'); if (!file.exists()) { // 如果文件不存在, 就需要創建 File parentFile = file.getParentFile(); parentFile.mkdirs(); } OutputStream out = new FileOutputStream(file); // 3. 讀取與寫入 byte[] buf = new byte[1024]; //分配1024個字節大小的內存給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:UsersJackmyDocly.jpg');

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

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

標簽: Java
相關文章:
主站蜘蛛池模板: 台湾a级理论片在线观看 | 日韩精品视频中文字幕 | 国产视频一二三区 | 97久久久久久 | 亚洲一区二区三区在线视频 | 中文字幕在线不卡 | 亚洲精品成人 | 国产精品黄视频 | 精品人伦一区二区三区蜜桃网站 | 成人午夜性成交 | 欧美一区二区三区视频在线播放 | 欧美999| 香蕉久久久 | 欧美精品日韩 | 一区二区国产精品 | 欧美一级三级在线观看 | 一级片网址 | 久久丁香 | 欧美一区二区免费视频 | 国产精品久久在线观看 | 视频1区 | 二区三区视频 | 精品在线一区 | 日韩午夜精品 | 日韩精品一区二区三区中文字幕 | 免费久久99精品国产婷婷六月 | 国产乱码精品一区二区三区五月婷 | 久久不卡 | 精品国产免费一区二区三区演员表 | 国产一区91精品张津瑜 | 精品在线一区 | 91免费电影 | 国产一区二区三区四区区 | 欧美国产激情二区三区 | 成人国产精品色哟哟 | 亚洲国产一区二区视频 | 少妇黄色 | 国产人成精品一区二区三 | 91av久久久 | 亚洲精品免费观看 | 日韩精品在线观看网站 |