java 20 -10 字节流四种方式复制mp3文件,测试效率
2024-10-19 12:39:00
电脑太渣,好慢。。反正速率是:
高效字节流一次读写一个字节数组 > 基本字节流一次读写一个字节数组 > 高效字节流一次读写一个字节 > 基本字节流一次读写一个字节 前两个远远快过后面2个
package zl_IOdemo; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; /*
* 需求:把D:\music\音乐\Beyond - 不再犹豫.mp3复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节:
* 基本字节流一次读写一个字节数组:
* 高效字节流一次读写一个字节:
* 高效字节流一次读写一个字节数组:
*/
public class CopyMp4 { public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//分别针对四种方式各创建一个方法,
//参数列表:String 数据源 String 目的地
//返回类型 void
method1("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
//method2("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
//method3("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
//method4("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
long end = System.currentTimeMillis();
System.out.println(end);
System.out.println("一共耗时"+(end - start)+"毫秒"); } private static void method4(String start , String end) throws IOException {
//高效字节流一次读写一个字节数组
BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(end));
byte[] by = new byte[1024];
int lend = 0;
while((lend = in.read(by)) != -1){
out.write(by,0,lend);
}
in.close();
out.close(); } private static void method3(String start , String end) throws IOException {
// 高效字节流一次读写一个字节
BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(end));
int lend = 0;
while(( lend = in.read()) != -1){
out.write(lend);
}
in.close();
out.close();
} private static void method2(String start , String end) throws IOException {
// 基本字节流一次读写一个字节数组
FileInputStream in = new FileInputStream(start);
FileOutputStream out = new FileOutputStream(end); byte[] by = new byte[1024];
int lend = 0;
while((lend = in.read(by)) != -1){
out.write(by,0,lend);
}
in.close();
out.close(); } private static void method1(String start , String end) throws IOException {
// 基本字节流一次读写一个字节
//创建基本字节输入流,以便从数据源读取文件
FileInputStream in = new FileInputStream(start);
//创建基本字节输出流,以便写入数据到目的地
FileOutputStream out = new FileOutputStream(end);
//复制目标文件
int i = 0;
while((i = in.read()) != -1){
out.write(i);
}
in.close();
out.close(); } }
最新文章
- (原创)解决.net 下使用uploadify,在火狐浏览器下的error 302
- leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java
- C# 线程同步
- Vector3.Lerp 插值
- DSP5509的时钟发生器(翻译总结自TI官方文档)
- css 控制滚动样式
- bzoj 1303: [CQOI2009]中位数图
- Light OJ Dynamic Programming
- VS Code C# 插件离线版 1.6.2
- ORACLE跨数据库查询的方法
- Codeforces 830C Bamboo Partition (看题解)
- HTML 第十二章总结
- Explain之key_len长度计算
- November 20th 2016 Week 47th Sunday
- 记一次centos6升级salt-minion启动失败的问题
- 【css】CSS3 Media Queries 详解【转】
- 安装并开启ssh服务
- caffe-安装anaconda后重新编译caffe报错
- 基于Requests和BeautifulSoup实现“自动登录”
- NO.3 Android SDK 高效更新