您的位置:首页 > 其它

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(); 
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: