文件拷贝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 ...
随机推荐
- 51Nod1766 树上的最远点对
1766 树上的最远点对 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i&l ...
- cloudera上面安装Spark2.0
Cloudera默认值是提供Spark1.6的安装,下面介绍如何来安装spark2.1 1. csd包:http://archive.cloudera.com/spark2/csd/ 2. parce ...
- BZOJ3039:玉蟾宫
浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...
- windows服务和进程的区别和联系
Windows Service 是主要用于服务器环境而长期运行的应用程序, 这类程序不需要有用户界面或者任何模拟输出. 任何的用户消息通常都是记录在Windows 事件日志里.Windows Serv ...
- redis多机集群部署文档
redis多机集群部署文档(centos6.2) (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下 ...
- 杂项-协议-HTTP:GET/POST/PUT/DELETE/INPUT/TRACE/OPTIONS/HEAD方法
ylbtech-杂项-协议-HTTP:GET/POST/PUT/DELETE/INPUT/TRACE/OPTIONS/HEAD方法 1.返回顶部 1. 请求方法是请求一定的Web页面的程序或用于特定的 ...
- Go和HTTPS
转自:http://tonybai.com/2015/04/30/go-and-https/ 近期在构思一个产品,考虑到安全性的原因,可能需要使用到HTTPS协议以及双向数字证书校验.之前只是粗浅接触 ...
- C#事件触发机制
C#的事件触发机制,类似于c++的回调函数机制 我先简单说一下,委托和事件的实质,后期再重开一篇博文来详细说 委托:指向方法的指针,类似于C的函数指针 事件:是一个可以存放0个或多个方法指针的数据结构 ...
- 问题:c# newtonsoft.json使用;结果:Newtonsoft.Json 用法
Newtonsoft.Json 用法 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: http://json.codeplex.com/ 使用方法 ...
- service的生命周期以及两种service的差异
可以看到,两种service的生命周期都相对简单,有一点不同的是,Intentservice每次调用的时候都执行onstartcommand,而boundservice一旦启动了之后,就不会每次执行o ...