package my;
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.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 文件拷贝各种方式的比较
*
*/
public class TestFile {
public final static String FILE_PATH ="D:\\电影\\[电影天堂www.dy2018.com]特殊身份BD中英双字.mkv";
//public final static String FILE_PATH = "F:\\apache-tomcat-7.0.11\\webapps\\ROOT\\a.rar";
public final static String FILE_PATH_OUT = "D:\\哈哈.mkv";
public static void TransByCommonIoStream() throws Exception {
long beginTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(new File(FILE_PATH));
FileOutputStream fos = new FileOutputStream(new File(FILE_PATH_OUT));
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.flush();
fis.close();
fos.close();
long endTime = System.currentTimeMillis();
System.out.println("采用传统IO FileInputStream 读取,耗时:"
+ (endTime - beginTime));
}
public static void TransByCommonIoBufferedStream() throws Exception {
long beginTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(new File(FILE_PATH));
FileOutputStream fos = new FileOutputStream(new File(FILE_PATH_OUT));
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
fis.close();
fos.close();
bis.close();
bos.close();
long endTime = System.currentTimeMillis();
System.out.println("采用传统IO BufferedInputStream 读取,耗时:"
+ (endTime - beginTime));
}
public static void TransByCommonIoBuffered() throws Exception {
long beginTime = System.currentTimeMillis();
Reader br = new BufferedReader(new FileReader(new File(FILE_PATH)));
Writer bw = new BufferedWriter(new FileWriter(new File(FILE_PATH_OUT)));
char[] c = new char[1024];
int len = 0;
while ((len = br.read(c)) != -1) {
bw.write(c, 0, len);
}
bw.flush();
br.close();
bw.close();
long endTime = System.currentTimeMillis();
System.out.println("采用传统IO BufferedReader 读取,耗时:"
+ (endTime - beginTime));
}
public static void TransByRandomAccFile() throws Exception {
long beginTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(new File(FILE_PATH));
RandomAccessFile raf = new RandomAccessFile(new File(FILE_PATH_OUT),
"rw");
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
raf.write(b, 0, len);
}
long endTime = System.currentTimeMillis();
System.out.println("采用传统IO RandomAccessFile 读取,耗时:"
+ (endTime - beginTime));
}
/**
* 采用FileChannel 自带方法测试 public abstract long
* transferFrom(ReadableByteChannel src, long position, long count) throws
* IOException;
*/
public static void TransByNioFileChannel() throws Exception {
long beginTime = System.currentTimeMillis();
FileChannel fc = new FileInputStream(new File(FILE_PATH)).getChannel();
// FileChannel fco = new RandomAccessFile(new File(FILE_PATH_OUT), "rw").getChannel();
FileChannel fco = new FileOutputStream(new File(FILE_PATH_OUT)).getChannel();
fco.transferFrom(fc, 0, fc.size());
long endTime = System.currentTimeMillis();
System.out.println("采用NIO FileChannel 自带方法 读取,耗时:"
+ (endTime - beginTime));
}
public static void TransByNioFileChannelCommon() throws Exception {
long beginTime = System.currentTimeMillis();
FileChannel fc = new FileInputStream(new File(FILE_PATH)).getChannel();
FileChannel fco = new RandomAccessFile(new File(FILE_PATH_OUT), "rw")
.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
while (fc.read(buf) != -1) {
buf.flip();
fco.write(buf);
buf.clear();
}
long endTime = System.currentTimeMillis();
System.out.println("采用NIO FileChannel 循环 读取,耗时:"
+ (endTime - beginTime));
}
public static void deleteFile() {
File f = new File(FILE_PATH_OUT);
if (f.exists())
f.delete();
}
public static void main(String[] args) throws Exception {
TransByCommonIoStream();
deleteFile();
TransByCommonIoBufferedStream();
deleteFile();
TransByRandomAccFile();
deleteFile();
TransByNioFileChannel();
deleteFile();
TransByNioFileChannelCommon();
deleteFile();
}
}
/**测试结果*/
采用传统IO FileInputStream 读取,耗时:11393
采用传统IO BufferedInputStream 读取,耗时:1191
采用传统IO RandomAccessFile 读取,耗时:2929
采用NIO FileChannel 自带方法 读取,耗时:485
采用NIO FileChannel 循环 读取,耗时:2502
分享到:
相关推荐
使用 io和nio 各个方式进行文件拷贝; 使用nio+byteBuffer 实现按行读取文件(大文件),在window/linux/macOS上均测试通过,对于中文乱码也已处理成功 完整注释,可随需求更改 有问题请邮件:mly610865580@126.com
- BIO 和 NIO 拷贝文件的区别 - 操作系统的零拷贝 - 选择器(Selectors) - 选择键(SelectionKey) - 示例:简易的客户端服务器通信 - 总结 Java IO 是一个庞大的知识体系,很多人学着学着就会学懵了,包括...
IO体系架构系统回顾与装饰模式的具体应用 33_Java NIO深入详解与体系分析 34_Buffer中各重要状态属性的含义与关系图解 35_Java NIO核心类源码解读与分析 36_文件通道用法详解 37_Buffer深入详解 38_NIO堆外内存与零...
51_NIO零拷贝彻底分析与Gather操作在零拷贝中的作用详解 52_NioEventLoopGroup源码分析与线程数设定 53_Netty对Executor的实现机制源码分析 54_Netty服务端初始化过程与反射在其中的应用分析 55_Netty提供的Future与...
第51讲:NIO零拷贝彻底分析与Gather操作在零拷贝中的作用详解 第52讲:NioEventLoopGroup源码分析与线程数设定 第53讲:Netty对Executor的实现机制源码分析 第54讲:Netty服务端初始化过程与反射在其中的应用分析...
IO 是主存和外部设备 ( 硬盘、终端和网络等 ) 拷贝数据的过程。 IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成。 所有语言运行时系统提供执行 I/O 较高级别的工具。 (c 的 printf scanf,java 的面向对象...
BIO 和 NIO 拷贝文件的区别操作系统的零拷贝 选择器(Selectors) 选择键(SelectionKey) 示例:简易的客户端服务器通信 集合 集合框架总览 -、Iterator Iterable ListIterator 二、Map 和 Collection 接口Map 集合体系...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
NIO中的特性之一就是零拷贝,在Java中,内存分为堆和栈以及字符串常量值等等,如果有一些数据从IO中读取并且放到堆里面,中间会经过一些缓冲区。 具体来讲,如果要从IO中读取数据,分为两个步骤: (1)从IO流中读取...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
高性能网络编程必备技能之IO与NIO阻塞分析.mp4 │ │ │ ├─9.JAVA并发编程之多线程并发同步业务场景与解决方案 │ │ 9.JAVA并发编程之多线程并发同步业务场景与解决方案.wmv │ │ │ ├─10.微服务架构之...
面试题包括以下十九部分:Java 基础、容器、多线程、反射、对象拷贝、Java Web 模块、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、Mybatis、RabbitMQ、Kafka、Zookeeper、MySql...
8. String str="i"与 String str=new String("i")一样吗? 5 9. 如何将字符串反转? 5 10. String 类的常用方法都有那些? 5 11. 抽象类必须要有抽象方法吗? 6 12. 普通类和抽象类有哪些区别? 6 13. 抽象类能使用 ...