文件拷贝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 ...
随机推荐
- CH#56C 异象石 和 BZOJ3991 [SDOI2015]寻宝游戏
异象石 CH Round #56 - 国庆节欢乐赛 描述 Adera是Microsoft应用商店中的一款解谜游戏. 异象石是进入Adera中异时空的引导物,在Adera的异时空中有一张地图.这张地图上 ...
- HTML a标签如何设置margin属性(转)
很多同学发现对DIV有效的许多CSS属性对<a>或<p>标签都无效,好比说 <div style="margin-top:5px;"></ ...
- python【事物 】【数据库锁】
1.数据库事物 1. 什么是事务 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一 ...
- JS之事件监听
一 如果事件监听类似于如下写法,则最终只会执行最后一个事件监听,其他监听都会被覆盖掉. window.onload=funtion(){console.log(1);}; window.onload= ...
- 【转】 Pro Android学习笔记(七四):HTTP服务(8):使用后台线程AsyncTask
目录(?)[-] 5秒超时异常 AsyncTask 实现AsyncTask抽象类 对AsyncTask的调用 在哪里运行 其他重要method 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注 ...
- Ubuntu18.04安装Docker, centos7安装Docker
Ubuntu18.04安装Docker 第一种方法从Ubuntu的仓库直接下载安装: 安装比较简单,这种安装的Docker不是最新版本,不过对于学习够用了,依次执行下面命令进行安装. $ sudo a ...
- 编译内核是出现:arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments
内核:Linux-3.4.2 编译内核出现arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments 交叉工具链太老了,换新一点的. ...
- asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践
最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅. 目前SpaceBuilder表现层使用是asp.net mvc v1.0,使 ...
- MY_SQLCode
一.SPC查询 根据日期查询 应用到了随机函数 NEWID()可以随机生成一个列值实现随机抓取记录 CONVERT(varchar(100),列名, 23) AS TestDat ...
- 网络编程之socket编程实例
简单实例1 server.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include ...