介绍一下Java NIO,NIO读取文件都有哪些方法

Python09

介绍一下Java NIO,NIO读取文件都有哪些方法,第1张

NIO也就是New I/O,是一组扩展Java IO操作的API集, 于Java 1.4起被引入,Java 7中NIO又提供了一些新的文件系统API,叫NIO2.

NIO2提供两种主要的文件读取方法:

使用buffer和channel类

使用Path 和 File 类

NIO读取文件有以下三种方式:

1. 旧的NIO方式,使用BufferedReader

import java.io.BufferedReader

import java.io.FileReader

import java.io.IOException

public class WithoutNIOExample

{

public static void main(String[] args)

{

BufferedReader br = null

String sCurrentLine = null

try

{

br = new BufferedReader(

new FileReader("test.txt"))

while ((sCurrentLine = br.readLine()) != null)

{

System.out.println(sCurrentLine)

}

}

catch (IOException e)

{

e.printStackTrace()

}

finally

{

try

{

if (br != null)

br.close()

} catch (IOException ex)

{

ex.printStackTrace()

}

}

}

}

2. 使用buffer读取小文件

import java.io.IOException

import java.io.RandomAccessFile

import java.nio.ByteBuffer

import java.nio.channels.FileChannel

public class ReadFileWithFileSizeBuffer

{

public static void main(String args[])

{

try

{

RandomAccessFile aFile = new RandomAccessFile(

"test.txt","r")

FileChannel inChannel = aFile.getChannel()

long fileSize = inChannel.size()

ByteBuffer buffer = ByteBuffer.allocate((int) fileSize)

inChannel.read(buffer)

buffer.rewind()

buffer.flip()

for (int i = 0i <fileSizei++)

{

System.out.print((char) buffer.get())

}

inChannel.close()

aFile.close()

}

catch (IOException exc)

{

System.out.println(exc)

System.exit(1)

}

}

}

3. 分块读取大文件

import java.io.IOException

import java.io.RandomAccessFile

import java.nio.ByteBuffer

import java.nio.channels.FileChannel

public class ReadFileWithFixedSizeBuffer

{

public static void main(String[] args) throws IOException

{

RandomAccessFile aFile = new RandomAccessFile

("test.txt", "r")

FileChannel inChannel = aFile.getChannel()

ByteBuffer buffer = ByteBuffer.allocate(1024)

while(inChannel.read(buffer) >0)

{

buffer.flip()

for (int i = 0i <buffer.limit()i++)

{

System.out.print((char) buffer.get())

}

buffer.clear()// do something with the data and clear/compact it.

}

inChannel.close()

aFile.close()

}

}

4. 使用MappedByteBuffer读取文件

import java.io.RandomAccessFile

import java.nio.MappedByteBuffer

import java.nio.channels.FileChannel

public class ReadFileWithMappedByteBuffer

{

public static void main(String[] args) throws IOException

{

RandomAccessFile aFile = new RandomAccessFile

("test.txt", "r")

FileChannel inChannel = aFile.getChannel()

MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size())

buffer.load()?

for (int i = 0i <buffer.limit()i++)

{

System.out.print((char) buffer.get())

}

buffer.clear()// do something with the data and clear/compact it.

inChannel.close()

aFile.close()

}

}

Java NIO : 同步非阻塞,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理。

Java AIO(NIO.2) : 异步非阻塞,服务器实现模式为一个有效请求一个线程,客户端的I/O请求都是由OS先完成了再通知服务器应用去启动线程进行处理,

NIO方式适用于连接数目多且连接比较短(轻操作)的架构,比如聊天服务器,并发局限于应用中,编程比较复杂,JDK1.4开始支持。

AIO方式使用于连接数目多且连接比较长(重操作)的架构,比如相册服务器,充分调用OS参与并发操作,编程比较复杂,JDK7开始支持

I/O属于底层操作,需要操作系统支持,并发也需要操作系统的支持,所以性能方面不同操作系统差异会比较明显。另外NIO的非阻塞,需要一直轮询,也是一个比较耗资源的。所以出现AIO