「java.nio教程」java nio tutorial

博主:adminadmin 2023-03-19 22:55:05 443

本篇文章给大家谈谈java.nio教程,以及java nio tutorial对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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

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 = 0; i fileSize; i++)

{

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 = 0; i 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 = 0; i 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.heapbytebuffer

heap buffer 和 direct buffer区别

在Java的NIO中,我们一般采用ByteBuffer缓冲区来传输数据,一般情况下我们创建Buffer对象是通过ByteBuffer的两个静态方法:

ByteBuffer.allocate(int capacity);

ByteBuffer.wrap(byte[] array);

查看JDK的NIO的源代码关于这两个部分:

/**allocate()函数的源码**/

public static ByteBuffer allocate(int capacity) {

if (capacity 0)

throw new IllegalArgumentException();

return new HeapByteBuffer(capacity, capacity);

}

/**wrap()函数的源码**/

public static ByteBuffer wrap(byte[] array) {

return wrap(array, 0, array.length);

}

//

public static ByteBuffer wrap(byte[] array,

int offset, int length)

{

try {

return new HeapByteBuffer(array, offset, length);

} catch (IllegalArgumentException x) {

throw new IndexOutOfBoundsException();

}

}

我们可以很清楚的发现,这两个方法都是实例化HeapByteBuffer来创建的ByteBuffer对象,也就是heap buffer. 其实除了heap buffer以外还有一种buffer,叫做direct buffer。我们也可以创建这一种buffer,通过ByteBuffer.allocateDirect(int capacity)方法,查看JDK源码如下:

public static ByteBuffer allocateDirect(int capacity) {

return new DirectByteBuffer(capacity);

}

我们发现该函数调用的是DirectByteBuffer(capacity)这个类,这个类就是创建了direct buffer。

如何学习Java的NIO?

Java NIO 是一种非阻塞式I/O。

要理解 NIO,要先理解 非阻塞式I/O。

非阻塞式I/O 只是众多 IO 模型中的一种。

要透切理解 非阻塞式I/O,就要理解众多 IO 模型的历史和优劣。

要理解为什么会出现 这么多 IO 模型。

就要理解 计算机的 IO 系统和 操作系统如何处理 IO的。

关于java.nio教程和java nio tutorial的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。