「javanio编程实例」java中nio使用案例

博主:adminadmin 2022-12-11 05:51:06 71

本篇文章给大家谈谈javanio编程实例,以及java中nio使用案例对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA NIO编程中,什么叫做通道事件就绪呢?

如果你将selector理解成一个不断循环的线程你就比较容易理解事件了,假设服务器的selector就是不断循环去判断每个链接到这里的Channel的状态发生了如何的变化。一旦Channel有了状态的变化,selector就发出相应的事件

java里面的NIO是什么,有什么用?

NIO即New IO,这个库是在JDK1.4中才引入的。NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多。

在Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。

介绍一下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使用的是水平触发还是边缘触发

水平触发(level-triggered,也被称为条件触发)LT: 只要满足条件,就触发一个事件(只要有数据没有被获取,内核就不断通知你)

边缘触发(edge-triggered)ET: 每当状态变化时,触发一个事件。

Java的NIO属于水平触发,即条件触发

这里介绍下水平触发和条件触发在IO编程的区别

举个读socket的例子,假定经过长时间的沉默后,现在来了100个字节,这时无论边缘触发和条件触发都会产生一个read ready notification通知应用程序可读。

应用程序读了50个字节,然后重新调用API等待io事件。这时条件触发的api会因为还有50个字节可读从 而立即返回用户一个read ready notification。

而边缘触发的api会因为可读这个状态没有发生变化而陷入长期等待。 因此在使用边缘触发的api时,要注意每次都要读到socket返回EWOULDBLOCK为止,否则这个socket就算废了。

而使用条件触发的API 时,如果应用程序不需要写就不要关注socket可写的事件,否则就会无限次的立即返回一个write ready notification。大家常用的select就是属于条件触发这一类,长期关注socket写事件会出现CPU 100%的毛病。

所以在使用Java的NIO编程的时候,在没有数据可以往外写的时候要取消写事件,在有数据往外写的时候再注册写事件。

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

The End

发布于:2022-12-11,除非注明,否则均为首码项目网原创文章,转载请注明出处。