IO之4种字节流拷贝文件方式对比
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\test.mp4", "copy4.mp4");
// method2("e:\\test.mp4", "copy4.mp4");
// method3("e:\\test.mp4", "copy4.mp4");
method4("e:\\test.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
字节流四种方式复制文件:
基本字节流一次读写一个字节: 共耗时:117235毫秒
基本字节流一次读写一个字节数组: 共耗时:156毫秒
高效字节流一次读写一个字节: 共耗时:1141毫秒
高效字节流一次读写一个字节数组: 共耗时:47毫秒
推荐使用第四种,即 高效字节流一次读写一个字节数组 方式读取文件。
IO之4种字节流拷贝文件方式对比的更多相关文章
- Selenium系列(十一) - 针对两种上传文件方式的实现方案
如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...
- 使用Java字节流拷贝文件
本文给出使用Java字节流实现文件拷贝的例子 package LearnJava; import java.io.*; public class FileTest { public static vo ...
- hadoop中两种上传文件方式
记录如何将本地文件上传至HDFS中 前提是已经启动了hadoop成功(nodedate都成功启动) ①先切换到HDFS用户 ②创建一个user件夹 bin/hdfs dfs -mkdir /user ...
- [JAVA]使用字节流拷贝文件
import java.io.*; /** * @Description: * @projectName:JavaTest * @see:PACKAGE_NAME * @author:郑晓龙 * @c ...
- [JAVA]字节流拷贝文件
import java.io.*; public class CopyFile { public static void main(String[] args) { //1.创建源 File in = ...
- js/css在html文档中的引用外部文件方式对比
包含css样式表和js脚本的最好方式是使用外部文件,因为css/js和html标记文档可以清晰地分离. css的外部引用写在<head></head>中: <head&g ...
- mui几种页面跳转方式对比
1.初始化时创建子页面 mui.init({ subpages: [{ url: your - subpage - url, //子页面HTML地址,支持本地地址和网络地址 id: your - su ...
- java IO之 File类+字节流 (输入输出 缓冲流 异常处理)
1. File类
- java——io、字节流缓冲区拷贝文件、字节缓冲流
使用try catch finally关闭文件流: 写入文件: import java.io.*; public class exp{ public static void main(String[] ...
随机推荐
- JDK1.8 HashMap 扩容 对链表(长度小于默认的8)处理时重新定位的过程
关于HashMap的扩容过程,请参考源码或百度. 我想记录的是1.8 HashMap扩容是对链表中节点的Hash计算分析. 对术语先明确一下: hash计算指的确定节点在table[index]中的链 ...
- python全栈 字典数据类型相关知识及操作
python 全栈开发 一.字典 1. 字典的概念: 字典 : dict 用 {} 来表示, 键位值数据. { key , value } 具有唯一性. 键:都必须是可哈希的 不可变 ...
- jenkin 不必要的Execute shell执行失败,导致jenkins都失败的解决
问题:jenkins里配置了多个执行shell,且有后续的执行job任务.但其中一个Execute shell执行失败了导致后续的shell都不执行了 而这个失败的shell并不是一定要执行 解决 ...
- npm run dev 自动打开浏览器
修改配置: config - index.js - autoOpenBrowser: true
- 1009 数字1的数量 数位dp
1级算法题就这样了,前途渺茫啊... 更新一下博客,我刚刚想套用数位dp的模板,发现用那个模板也是可以做到,而且比第二种方法简单很多 第一种方法:我现在用dp[pos][now]来表示第pos位数字为 ...
- unity 3d音效如何设置?,近大远小
请问怎么将导入的音频文件设置为“3D Sound”?这是我导入的音频文件: 现在3D音效的设置不在导入音效文件的地方,而是在AudioSource的SpatialBlend属性,0表示2D,1表示3D ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- DataTable 作为ObjectDataSource的数据源
好像是不能直接实现的.不过我从网上找到了这样的方法: 比如在BLL层中,SubjectManager是我对Subject表的业务类,此类中包含了Subject表的增删改查等各种方法.在aspx 页面中 ...
- swift - 本地通知
1. AppDelegate 注册 class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? fun ...
- Wechat微信公众平台开发
一.微信概述 1.历史背景 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册用户量突破 ...