NIO与IO的区别
2015-10-16 10:37
183 查看
NIO是面向缓冲区的,IO是面向流的。
IO面向流每次从流中读取一个或多个字节,直至到读取所有的字节。不能前后移动流中的数据。如果余姚前后移动从流中读取数据,先要将它缓存到一个缓存区中。
NIO将数据读取到一个它稍后处理的缓冲区中,需要时可在缓冲区中前后移动。
IO的各种流是阻塞的。当一个线程调用read()或write()时,该线程被阻塞,直到有一些数据被读取,或数据被完全写入。该线程在此期间不能再干任何事情。
NIO是非阻塞模式,使一个线程冲某个通道发送请求读取数据,但是它仅能得到目前的可用的数据,如果没有数据可用时,就什么都不会获得。而不是保持线程阻塞,该线程可以做其他事。
实例:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class FileCopyTest {
public static void main(String[] args) throws Exception {
String sourcePath = "F:/dir1/apache-maven-3.2.1-bin.zip"; //文件目录
String destPath1 = "F:/dir2/apache-maven-3.2.1-bin1.zip"; //目标文件目录
String destPath2 = "F:/dir2/apache-maven-3.2.1-bin2.zip";
String destPath3 = "F:/dir2/apache-maven-3.2.1-bin3.zip";
long t1 = System.currentTimeMillis();
traditionalCopy(sourcePath,destPath1);
long t2 = System.currentTimeMillis();
System.out.println("传统IO方法实现文件拷贝耗时:" + (t2-t1) + "ms");
nioCopy(sourcePath,destPath2);
long t3 = System.currentTimeMillis();
System.out.println("利用NIO文件通道方法实现文件拷贝耗时:" + (t3-t2) + "ms");
nioCopy2(sourcePath,destPath3);
long t4 = System.currentTimeMillis();
System.out.println("利用NIO文件内存映射及文件通道实现文件拷贝耗时:" + (t4-t3) + "ms");
}
private static void traditionalCopy(String sourcePath, String destPath) throws Exception{
File source = new File(sourcePath);
File dest = new File(destPath);
if(!dest.exists()){
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
byte [] buf = new byte [512];
int len = 0;
while((len =fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
@SuppressWarnings("resource")
private static void nioCopy(String sourcePath, String destPath) throws Exception{
File source = new File(sourcePath);
File dest = new File(destPath);
if(!dest.exists()){
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
FileChannel sourceCh = fis.getChannel();
FileChannel destCh = fos.getChannel();
destCh.transferFrom(sourceCh, 0, sourceCh.size());
sourceCh.close();
destCh.close();
}
@SuppressWarnings("resource")
private static void nioCopy2(String sourcePath, String destPath) throws Exception {
File source = new File(sourcePath);
File dest = new File(destPath);
if(!dest.exists()){
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
FileChannel sourceCh = fis.getChannel();
FileChannel destCh = fos.getChannel();
MappedByteBuffer mbb = sourceCh.map(FileChannel.MapMode.READ_ONLY, 0, sourceCh.size());
destCh.write(mbb);
sourceCh.close();
destCh.close();
}
}
IO面向流每次从流中读取一个或多个字节,直至到读取所有的字节。不能前后移动流中的数据。如果余姚前后移动从流中读取数据,先要将它缓存到一个缓存区中。
NIO将数据读取到一个它稍后处理的缓冲区中,需要时可在缓冲区中前后移动。
IO的各种流是阻塞的。当一个线程调用read()或write()时,该线程被阻塞,直到有一些数据被读取,或数据被完全写入。该线程在此期间不能再干任何事情。
NIO是非阻塞模式,使一个线程冲某个通道发送请求读取数据,但是它仅能得到目前的可用的数据,如果没有数据可用时,就什么都不会获得。而不是保持线程阻塞,该线程可以做其他事。
实例:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class FileCopyTest {
public static void main(String[] args) throws Exception {
String sourcePath = "F:/dir1/apache-maven-3.2.1-bin.zip"; //文件目录
String destPath1 = "F:/dir2/apache-maven-3.2.1-bin1.zip"; //目标文件目录
String destPath2 = "F:/dir2/apache-maven-3.2.1-bin2.zip";
String destPath3 = "F:/dir2/apache-maven-3.2.1-bin3.zip";
long t1 = System.currentTimeMillis();
traditionalCopy(sourcePath,destPath1);
long t2 = System.currentTimeMillis();
System.out.println("传统IO方法实现文件拷贝耗时:" + (t2-t1) + "ms");
nioCopy(sourcePath,destPath2);
long t3 = System.currentTimeMillis();
System.out.println("利用NIO文件通道方法实现文件拷贝耗时:" + (t3-t2) + "ms");
nioCopy2(sourcePath,destPath3);
long t4 = System.currentTimeMillis();
System.out.println("利用NIO文件内存映射及文件通道实现文件拷贝耗时:" + (t4-t3) + "ms");
}
private static void traditionalCopy(String sourcePath, String destPath) throws Exception{
File source = new File(sourcePath);
File dest = new File(destPath);
if(!dest.exists()){
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
byte [] buf = new byte [512];
int len = 0;
while((len =fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
@SuppressWarnings("resource")
private static void nioCopy(String sourcePath, String destPath) throws Exception{
File source = new File(sourcePath);
File dest = new File(destPath);
if(!dest.exists()){
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
FileChannel sourceCh = fis.getChannel();
FileChannel destCh = fos.getChannel();
destCh.transferFrom(sourceCh, 0, sourceCh.size());
sourceCh.close();
destCh.close();
}
@SuppressWarnings("resource")
private static void nioCopy2(String sourcePath, String destPath) throws Exception {
File source = new File(sourcePath);
File dest = new File(destPath);
if(!dest.exists()){
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
FileChannel sourceCh = fis.getChannel();
FileChannel destCh = fos.getChannel();
MappedByteBuffer mbb = sourceCh.map(FileChannel.MapMode.READ_ONLY, 0, sourceCh.size());
destCh.write(mbb);
sourceCh.close();
destCh.close();
}
}
相关文章推荐
- jQuery实现简易的天天爱消除小游戏
- The Storage Situation: External Storage
- 在Arcgis中基于Python对地图分级别进行四色填充
- C#中关于Gridview的一些属性(一)
- jsonp使用
- 浅谈站群的再次重生带来的效果到底如何
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索
- 每天一个linux命令---telnet
- Nexus 5双清
- android 几个经常用到的字符串的截取
- Jenkins 安装及配置 1
- Core Animation系列之CADisplayLink
- css 设计与布局
- ajax请求--触发点击事件
- OpenStack Mnaila 2015-10-16 IRC会议内容
- FTP下载文件实例
- ResponseProcessCookies:121 - Cookie rejected问题
- ping检测整个网络的主机是否有响应
- Python的对象和类型
- 使用正则表达式NSRegularExpression 来验证textfiled输入的内容