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

最新文章

  1. Redis安装测试(待完善)
  2. 启动tomcat部署项目时 ContainerBase.addChild: start:
  3. Sumsets
  4. js图片时间翻转
  5. HDU 5628 Clarke and math Dirichlet卷积+快速幂
  6. ar解压deb包
  7. ExtJS001HelloWorld弹窗
  8. 关于Unity中NGUI图片精灵响应鼠标的方法
  9. Dynamics 365 CE的插件/自定义工作流活动中调用Web API示例代码
  10. C#使用List实现类似RadioButtonGroup的单选功能
  11. 在Java的Condition接口【唤醒全部线程】
  12. 20170921xlVBA_SQL蒸发循环查询2
  13. 收藏:IPicture总结
  14. 2017广东工业大学程序设计竞赛决赛 Problem E: 倒水(Water) (详解)
  15. 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
  16. 0302借软件工程触IT
  17. rm 删除不掉文件,报错解决 以及 chattr的介绍
  18. HDU 1372 Knight Moves (广搜)
  19. 手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统系列博文索引
  20. iOS中的数据存储

热门文章

  1. photonView 空指针异常
  2. 随机数 while循环 do while循环 for循环
  3. javaScript之深度理解原型链
  4. 最近一直是web前段,没什么意思,所以就不发资料了
  5. 【总结整理】关于Json的解析,校验和验证
  6. Angular13 Angular2发送PUT请求在后台接收不到参数
  7. GC偏好的校正与偏好程度的评估
  8. storm定时器timer源码分析-timer.clj
  9. Spring入门第二课
  10. C#中索引器Indexer的学习使用