java nio原理和它的优点

    要想讲清楚nio的原理和它的优点得先清楚Java应用程序的文件读写原理和虚拟内存的原理。Java文件读取原理可参见如下图:


当应用程序需要读取文件的时候,内核首先通过DMA技术将文件内容从磁盘读入内核中的buffer,然后Java应用进程再从内核的buffer将数据读取到应用程序的buffer。

为了提升I/O效率和处理能力,操作系统采用虚拟内存的机制。虚拟内存也就是我们常说的交换内存,它实际上是硬盘上的文件,虚拟内存有两个作用:

1. 不同的虚拟内存可以映射到相同的物理内存,根据这个原理,可以简化文件读取流程,提升读取效率,效果如下图所示:


通过使用虚拟内存技术,将应用程序的buffer和内核的buffer都作为虚拟内存,并且两块不同的虚拟内存指向相同的物理内存,内核通过DMA将数据读取到buffer的时候,应用程序就可以直接使用这些数据了。

2. 通过使用虚拟内存, 应用程序可以使用比物理内存所能容纳得大得多的内存,并且也能够提高I/O效率。当物理内存中的数据不使用的时候,可以将物理内存中的数据放到虚拟内存中,操作系统就可以腾出物理内存空间存储新的要处理的数据。当需要使用虚拟内存中的数据时,再可以把虚拟内存中的数据加载到物理内存中。因此物理内存可以看做时虚拟内存中存放数据的cache。而上面所述的cache虚拟内存数据的过程,对应用程序来说时透明的,它可以像处理物理内存数据一样处理虚拟内存中的数据。


Java nio通过使用虚拟内存技术将文件系统中的文件页和应用程序空间直接对应起来,使用nio后,文件的读写操作都是在虚拟内存中实现。这样在操作文件的时候,好像文件已经在内存中一样。采用了虚拟内存技术,使用Java nio方式可以很快的读取很大的文件。

Java nio打开文件可指定映射内容在所要映射的文件所在位置。使用Java nio打开文件源码:

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
 
public class MemoryMappedFileExample
{
    static int length = 0x8FFFFFF; // 128 Mb
 
    public static void main(String[] args) throws Exception
    {
        MappedByteBuffer out = new RandomAccessFile("howtodoinjava.dat", "rw")
                                    .getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
        for (int i = 0; i < length; i++)
        {
            out.put((byte) 'x');
        }
        System.out.println("Finished writing");
    }
}



使用Java nio读取文件内容源码:

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
 
public class MemoryMappedFileReadExample
{
    private static String bigExcelFile = "bigFile.xls";
 
    public static void main(String[] args) throws Exception
    {
        //Create file object
        File file = new File(bigExcelFile);
         
        //Get file channel in readonly mode
        FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
         
        //Get direct byte buffer access using channel.map() operation
        MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
         
        // the buffer now reads the file as if it were loaded in memory.
        System.out.println(buffer.isLoaded());  //prints false
        System.out.println(buffer.capacity());  //Get the size based on content size of file
         
        //You can read the file from this buffer the way you like.
        for (int i = 0; i < buffer.limit(); i++)
        {
            System.out.print((char) buffer.get()); //Print the content of file
        }
    }
}


使用Java nio写入文件新的内容:

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
 
public class MemoryMappedFileWriteExample {
    private static String bigExcelFile = "test.txt";
 
    public static void main(String[] args) throws Exception {
        // Create file object
        File file = new File(bigExcelFile);
         
        //Delete the file; we will create a new file
        file.delete();
 
        // Get file channel in readonly mode
        FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
 
        // Get direct byte buffer access using channel.map() operation
        MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 4096 * 8 * 8);
 
        //Write the content using put methods
        buffer.put("howtodoinjava.com".getBytes());
    }
}


可见Java nio的优势为:

1. 不需要使用read()或者write()操作就可以处理文件内容了

2. 修改文件后,修改自动flush到文件

3. nio方式能很快处理大文件和处理效率很快


参考文章:

http://howtodoinjava.com/2014/12/10/how-java-io-works-internally-at-lower-level/

http://howtodoinjava.com/2015/01/16/java-nio-2-0-memory-mapped-files-mappedbytebuffer-tutorial/







  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值