NIO与I/O的区别
- 博客分类:
- NIO使用
NIO与原有的I/O有同样的作用和目的,是基于原有的I/O的改进和扩展。NIO与原有的I/O不同,他是基于特殊的缓冲块进行的搞笑I/O操作。NIO的缓冲区块与普通的缓冲区不同,他是一块连续的空间,他的内存的分配不再java的
堆栈中,不受java内存的回收的影响;他的实现不是纯java代码,而是本地代码,这样操作系统,可以直接与缓冲区进行交互,java程序只需要完成对缓冲区的读写,而后续操作由操作系统完成。
使用NIO来实现对文件的复制。效率比一般的IO流快很多。在使用NIO的时候采用了缓冲刘的封装,所以效率会更高。
以下是利用NIO实现对文件的复制
- 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]);
- }
- }
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的可移植性。