文件拷贝io nio比较
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files; public class FileCopy {
public static void fileCopyByByte(String inPah, String outPah)
throws FileNotFoundException, IOException {
byte[] byteArray = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
inPah));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(outPah));
int readCount = 0;
while ((readCount = bis.read(byteArray)) != -1) {
bos.write(byteArray, 0, readCount);
bos.flush();
}
bis.close();
bos.close();
} public static void fileCopyByChar(String inPah, String outPah)
throws FileNotFoundException, IOException {
char[] charArray = new char[1024];
BufferedReader reader = new BufferedReader(new FileReader(inPah));
BufferedWriter writer = new BufferedWriter(new FileWriter(outPah));
int readCount = 0;
while ((readCount = reader.read(charArray)) != -1) {
writer.write(charArray, 0, readCount);
writer.flush();
}
reader.close();
writer.close();
} public static void fileCopyByFileChannel(String inPah,String outPah) throws FileNotFoundException,IOException{
FileInputStream fis = new FileInputStream(inPah);
FileOutputStream fos = new FileOutputStream(outPah);
FileChannel fileChannel_from = fis.getChannel();
FileChannel fileChannel_to = fos.getChannel(); ByteBuffer bytebuffer = ByteBuffer.allocate(1024); // Read data from file into ByteBuffer
int bytesCount;
while ((bytesCount = fileChannel_from.read(bytebuffer)) > 0) {
//flip the buffer which set the limit to current position, and position to 0
bytebuffer.flip();
//write data from ByteBuffer to file
fileChannel_to.write(bytebuffer);
//for the next read
bytebuffer.clear();
}
fileChannel_from.close();
fileChannel_to.close();
}
public static void fileCopyByFileChannelMap(String inPah,String outPah) throws FileNotFoundException,IOException{
FileInputStream fis = new FileInputStream(inPah);
FileOutputStream fos = new FileOutputStream(outPah);
FileChannel fileChannel_from = fis.getChannel();
FileChannel fileChannel_to = fos.getChannel(); MappedByteBuffer bytebuffer = fileChannel_from.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel_from.size());
fileChannel_to.write(bytebuffer);
bytebuffer.clear();
fileChannel_from.close();
fileChannel_to.close();
} public static void main(String[] args) {
try {
String in = "E:/小说/左道旁门.txt";
long begin = System.currentTimeMillis();
fileCopyByByte(in, "e:/2");
System.out.println(System.currentTimeMillis() - begin);
begin = System.currentTimeMillis();
fileCopyByFileChannel(in, "e:/3");
System.out.println(System.currentTimeMillis() - begin);
begin = System.currentTimeMillis();
fileCopyByFileChannelMap(in, "e:/4");
System.out.println(System.currentTimeMillis() - begin);
begin = System.currentTimeMillis();
Files.copy(new File(in).toPath(), new File("e:/5").toPath());
System.out.println(System.currentTimeMillis() - begin); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
十几M小文件
360
843
672
641
100多M文件
19547
5610
2703
8718
300多M文件
41156
13609
8563
9500
1.7G文件
202156
225109
出错,可能超过限制
163719
文件拷贝io nio比较的更多相关文章
- Java IO和Java NIO在文件拷贝上的性能差异分析
1. 在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...
- Java IO和Java NIO 和通道 在文件拷贝上的性能差异分析
1. 在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...
- 文件拷贝, 使用 BIO,NIO的对比,四种写法性能分析。
测试环境: jdk 1.7 + 2G内存 测试代码基本上复制了: http://blog.csdn.net/tabactivity/article/details/9317143 1 2 3 4 5 ...
- BIO与NIO的方式实现文件拷贝
面试题 - 编程实现文件拷贝.(这个题目在笔试的时候经常出现,下面的代码给出了两种实现方案) import java.io.FileInputStream; import java.io.FileOu ...
- 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇
本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...
- Java通过NIO实现快速文件拷贝的代码
将内容过程重要的内容片段做个记录,下面的内容段是关于Java通过NIO实现快速文件拷贝的内容. public static void fileCopy( File in, File out ) thr ...
- 38、使用IO流进行文件拷贝
使用IO流进行文件拷贝 需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件先以流的方式将java.txt文件读取到内存中,然 ...
- Java IO编程——文件拷贝
在操作系统里面有一个copy命令,这个命令的主要功能是可以实现文件的拷贝处理,现在要求模拟这个命令,通过初始化参数输入拷贝的源文件路径与拷贝的目标路径实现文件的拷贝处理. 需求分析: ·需要实现文件的 ...
- IO流文件拷贝
目录 IO流文件拷贝 前言 字节流(使用FileInputStream和FileOutputStream读取每一个字节...) 字节流(使用FileInputStream和FileOutputStre ...
随机推荐
- JS字符串类型转日期然后进行日期比较
1.字符串转日期格式 var stringToDate = function(dateStr,separator){ if(!separator){ separator="-"; ...
- properties使用
properties可以load store 注释可以采用 "#" 或者"!" 分隔采用"="或者":" 分行采用&qu ...
- [转]HTTP头的Expires与Cache-control
1.概念 Cache-control用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache) 数据包中的格式: Cache-Control: cache- ...
- VIJOS:P1706(舞会)
描述 Arthur公司是一个等级森严的公司,它们有着严格的上司与下属的关系,公司以总裁为最高职位,他有若干个下属,他的下属又有若干个下属,他的下属的下属又有若干个下属……现接近年尾,公司组织团拜活动, ...
- windows服务和进程的区别和联系
Windows Service 是主要用于服务器环境而长期运行的应用程序, 这类程序不需要有用户界面或者任何模拟输出. 任何的用户消息通常都是记录在Windows 事件日志里.Windows Serv ...
- 江苏省大学生程序设计竞赛(JSCPC)赛后感
仔细的算了一下,这是我第6次参加与acm有关的比赛了,每一次的经历,在我看来都是一次对自己能力的认识与评估,身边学计算机专业的同龄人对这项比赛的热爱,专注,勇气以及所获得的成就让要好好努力,更要加倍付 ...
- 关于jquery登录的一些简单验证。
获取值之后的判断 $(function () { $("#btlogin").click(function () { var txtaccount = $("#txtac ...
- c语言基础 c和指针
句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...
- BZOJ4085: [Sdoi2015]音质检测
BZOJ4085: [Sdoi2015]音质检测 由于这题太毒了,导致可能会被某些人卡评测,于是成了一道权限题... 本蒟蒻表示没钱氪金... 但是可以去洛谷/Vijos搞搞事... 但是洛谷上只能评 ...
- Koa1 框架
安装创建项目: 1.一定要全局安装(koa1.2和koa2都己经支持) npm install koa-generator -g 2.koa1 生成一个test项目,切到test目录并下载依赖 koa ...