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比较的更多相关文章

  1. Java IO和Java NIO在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...

  2. Java IO和Java NIO 和通道 在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...

  3. 文件拷贝, 使用 BIO,NIO的对比,四种写法性能分析。

    测试环境: jdk 1.7 +  2G内存 测试代码基本上复制了: http://blog.csdn.net/tabactivity/article/details/9317143 1 2 3 4 5 ...

  4. BIO与NIO的方式实现文件拷贝

    面试题 - 编程实现文件拷贝.(这个题目在笔试的时候经常出现,下面的代码给出了两种实现方案) import java.io.FileInputStream; import java.io.FileOu ...

  5. 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  6. Java通过NIO实现快速文件拷贝的代码

    将内容过程重要的内容片段做个记录,下面的内容段是关于Java通过NIO实现快速文件拷贝的内容. public static void fileCopy( File in, File out ) thr ...

  7. 38、使用IO流进行文件拷贝

    使用IO流进行文件拷贝 需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件先以流的方式将java.txt文件读取到内存中,然 ...

  8. Java IO编程——文件拷贝

    在操作系统里面有一个copy命令,这个命令的主要功能是可以实现文件的拷贝处理,现在要求模拟这个命令,通过初始化参数输入拷贝的源文件路径与拷贝的目标路径实现文件的拷贝处理. 需求分析: ·需要实现文件的 ...

  9. IO流文件拷贝

    目录 IO流文件拷贝 前言 字节流(使用FileInputStream和FileOutputStream读取每一个字节...) 字节流(使用FileInputStream和FileOutputStre ...

随机推荐

  1. P2056 采花

    题目描述 萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成 ...

  2. bzoj 1070 修车 —— 费用流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1070 需要考虑前面修的车对后面等待的车造成的时间增加: 其实可以从每个人修车的顺序考虑,如果 ...

  3. Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)

    一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...

  4. 上海-北京间通过Azure Storage的RA-GRS类型的存储账户进行快速复制

    Azure的Blob存储分成多种类型,目前主要有: 其中RA-GRS可以在上海-北京两个数据中心间同步数据.并且,在第二个数据中心可以只读的方式读取这个存储账户中的Blob内容. 虽然GRS采用的是准 ...

  5. HDOJ1073(gets 应用)

    练习操作字符串的好题. #include<cstdio> #include<algorithm> #include<cstring> using namespace ...

  6. #np.random.normal,产生制定分布的数集(默认是标准正态分布)

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html #np.random.normal,产生制定分 ...

  7. USB插拔检测程序

    一.手动添加ON_WM_DEVICECHANGE()消息 二.添加头文件#include <Dbt.h> 三.定义设备的GUID static const GUID GUID_DEVINT ...

  8. 【问题】Expandable数据集的定义的正确方法,TabActivity弃用替代,Gallery替代,imageswitcher

    Expandable 问题: http://www.cnblogs.com/xingyyy/p/3389611.html 扩展阅读:http://blog.csdn.net/lmj623565791/ ...

  9. JQuery 1.6之后,获取属性推荐用prop

    今天在做界面,要获取CheckBox的是否被选中, var ck=$("#id").attr("checked"); 但是这样取得的值是undefined, 查 ...

  10. linux 安装输入法

    简述 Ubuntu16.04安装完后,和12.04以及14.04都不一样,并没有中文输入功能.于是搜索一些安装中文输入法的方法. 开始安装了ibus pinyin输入法,但是系统重启之后发现有些时候不 ...