NIO与I/O的区别


NIO与I/O的区别
nio io java 

NIO与原有的I/O有同样的作用和目的,是基于原有的I/O的改进和扩展。NIO与原有的I/O不同,他是基于特殊的缓冲块进行的搞笑I/O操作。NIO的缓冲区块与普通的缓冲区不同,他是一块连续的空间,他的内存的分配不再java的

堆栈中,不受java内存的回收的影响;他的实现不是纯java代码,而是本地代码,这样操作系统,可以直接与缓冲区进行交互,java程序只需要完成对缓冲区的读写,而后续操作由操作系统完成。

使用NIO来实现对文件的复制。效率比一般的IO流快很多。在使用NIO的时候采用了缓冲刘的封装,所以效率会更高。

以下是利用NIO实现对文件的复制

 

Java代码 复制代码  收藏代码
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.nio.ByteBuffer;  
  5. import java.nio.channels.FileChannel;  
  6.   
  7. public class CopyFile {  
  8.   
  9.     /** 
  10.      * 实现文件复制 
  11.      *  
  12.      * @param srcFile 
  13.      *            srcFile源文件 
  14.      * @param destFile 
  15.      *            destFile目标文件 
  16.      * @throws IOException 
  17.      */  
  18.     public void copy(String srcFile, String destFile) throws IOException {  
  19.         long begin = System.currentTimeMillis();  
  20.         // 创建ByteBuffer缓冲区,大小可以根据实际情况指定  
  21.         ByteBuffer buffer = ByteBuffer.allocate(1024);  
  22.         // 获取 NIO读取通道  
  23.         FileInputStream fin = null;  
  24.         FileChannel in = null;  
  25.         // 获取NIO写入通道  
  26.         FileOutputStream fout = null;  
  27.         FileChannel out = null;  
  28.   
  29.         fin = new FileInputStream(srcFile);  
  30.         in = fin.getChannel();  
  31.         fout = new FileOutputStream(destFile);  
  32.         out = fout.getChannel();  
  33.         int len = -1;  
  34.         // 当文件读到末尾时候结束循环  
  35.         while ((len = in.read(buffer)) != -1) {  
  36.             // 在write之前,将position和limit标志设置好  
  37.             buffer.flip();  
  38.             // 按照设定的position位置开始读,到limit结束  
  39.             out.write(buffer);  
  40.             // 初始化position/limit/capcity标志的位置,为下一次循环读取做准备  
  41.             buffer.clear();  
  42.         }  
  43.         long end = System.currentTimeMillis();  
  44.         // 根据前后时间之差,计算复制文件所需的时间  
  45.         System.out.println(end - begin);  
  46.         out.close();  
  47.         in.close();  
  48.         fout.close();  
  49.         fin.close();  
  50.   
  51.     }  
  52.   
  53.     /** 
  54.      * Description 
  55.      *  
  56.      * @param args 
  57.      * @throws IOException 
  58.      */  
  59.     public static void main(String[] args) throws IOException {  
  60.         CopyFile copyFile = new CopyFile();  
  61.         copyFile.copy(args[0], args[1]);  
  62.     }  
  63.   
  64. }  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyFile {

	/**
	 * 实现文件复制
	 * 
	 * @param srcFile
	 *            srcFile源文件
	 * @param destFile
	 *            destFile目标文件
	 * @throws IOException
	 */
	public void copy(String srcFile, String destFile) throws IOException {
		long begin = System.currentTimeMillis();
		// 创建ByteBuffer缓冲区,大小可以根据实际情况指定
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		// 获取 NIO读取通道
		FileInputStream fin = null;
		FileChannel in = null;
		// 获取NIO写入通道
		FileOutputStream fout = null;
		FileChannel out = null;

		fin = new FileInputStream(srcFile);
		in = fin.getChannel();
		fout = new FileOutputStream(destFile);
		out = fout.getChannel();
		int len = -1;
		// 当文件读到末尾时候结束循环
		while ((len = in.read(buffer)) != -1) {
			// 在write之前,将position和limit标志设置好
			buffer.flip();
			// 按照设定的position位置开始读,到limit结束
			out.write(buffer);
			// 初始化position/limit/capcity标志的位置,为下一次循环读取做准备
			buffer.clear();
		}
		long end = System.currentTimeMillis();
		// 根据前后时间之差,计算复制文件所需的时间
		System.out.println(end - begin);
		out.close();
		in.close();
		fout.close();
		fin.close();

	}

	/**
	 * Description
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		CopyFile copyFile = new CopyFile();
		copyFile.copy(args[0], args[1]);
	}

}
 

NIO是在原有的IO的基础上进行改进,因此使用NIO来读写文件,首先还是需要使用IO流类。不是所有的IO流都支持NIO操作,支持NIO的操作的类有FileInputStream、FileOutputStream和RandomAccessFile。

 

NIO并不是对原有的I/O的替代,尽管NIO在I/O操作时速度快,但是由于其底层借助了大量的本地代码,对操作系统和硬件平台有很大的依赖性,浙江影响Java的可移植性。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值