首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java nio(1)

2012-10-30 
Java nio(一)jdk1.4提供了java.nio包,为从根本上改善I/O的性能提供了可能,但是nio要比以前的I/O要复杂,提

Java nio(一)
jdk1.4提供了java.nio包,为从根本上改善I/O的性能提供了可能,但是nio要比以前的I/O要复杂,提供了更底层的操作和更细的api。学起来并不是那么快就上手,有专门一本书
介绍nio的。我希望通过总结更好的梳理整个nio框架各个类之间的关系,从而能够灵活的使用nio包。
nio通常需要涉及到三个对象:
1、数据源:从文件中获得的FileInputStream/FileOutputStream、从socket中获得的输入输出流等
2、缓冲区对象(buffer):nio定义了一系列buffer对象,最基本的是ByteBuffer,其他还有各种基本数据类型除了boolean类型外,所对应的buffer类型。使用这些buffer一般的过程是:从数据源中读到ByteBuffer中,然后使用ByteBuffer.asXxxBuffer()转换成特定的基本类型,然后对这个基本类型的buffer进行操作;把基本类型转换成ByteBuffer类型,然后写入数据源中。
3、通道对象(channel):他提供了对数据源的一个连接,可以从文件中获得FileChannel,从Socket中获得SocketChannel,它是一个中介。提供了数据源与缓冲区之间的读写操作。
我们可以同过一句话来解释他们三者之间的关系:
channel对象提供了数据源与缓冲区之间的读写操作,是数据源与缓冲区的一个桥梁,通过这个桥梁,数据在两者之间流动。
首先我们看看这个中介Channel类的整个结构:

Channel < ClosableWritableByteChannel < ChannelInterruptibleChannel < ChannelReadableByteChannel < ChannelGetherByteChannel < WritableByteChannel ByteChannel < WritableByteChannel,ReadableByteChannel ScatteringByteChannel < ReadableByteChannel 


Channel接口声明了两个方法:
close(),用来关闭通道
isOpen(),用于测试通道是否打开
其他的通道:
ByteChannel只是简单的合并了WritableByteChannel和ReadableChannel的,ScatteringByteChannel接口扩展了ReadableChannel接口,添加了一个读取并将数据
分布在不同缓冲区的方法,而GatheringByteChannel接口在WritableByteChannel的基础上添加了将多个独立的缓冲区写入驱动器中的方法。
Closeable:void close()//关闭源或目的地,并释放资源Channel:void close()//关闭通道void boolean isOpen()//判断通道是否打开ReadableByteChannel:int read(ByteBuffer input)//将字节从通道读入input缓冲区中,返回读取的字节数,到达流结尾时,返回-1WritableByteChannel:int write(ByteBuffer output)//将字节从实参output缓冲区写入通道,返回写的字节数ByteChannel 仅仅继承了ReadableByteChannel和WritableByteChannel的方法ScatteringByteChannel:int read(ByteBuffer[] inputs)//将字节从通道读入inputs缓冲区数组中,返回读取的字节,到达流末尾是,返回-1int read(ByteBuffer[] inputs,int offset,int length)//将字节从通道读inputs[offset]---inputs[offeset+length-1]缓冲区中GatherByteChannel:int write(ByteBuffer[] outputs);//把outputs缓存区数组写入通道中int write(ByteBuffer[] outputs,int offset,int length);把从outputs[offset]-->outputs[offset+length-1]缓冲区写入通道中

例子:(我们忽略了各种Exception)
File file = new File("C:\\test.txt");FileOutputStream fos = new FileOutputStream(file);FileChannel outputChannel = fos.getChannel();String str = "Hello,just a test";ByteBuffer bb = ByteBuffer.wrap(str.getBytes());outputChannel.write(bb);

先总结到这,下次总结各种Buffer及其操作。# File file = new File("C:\\test.txt"); # FileOutputStream fos = new FileOutputStream(file); # FileChannel outputChannel = fos.getChannel(); # String str = "Hello,just a test"; # ByteBuffer bb = ByteBuffer.wrap(str.getBytes()); # outputChannel.write(bb);
与不使用FileChannel的具体区别么?

-dongtalk@gmail.com
# File file = new File("C:\\test.txt"); # FileOutputStream fos = new FileOutputStream(file); # FileChannel outputChannel = fos.getChannel(); # String str = "Hello,just a test"; # ByteBuffer bb = ByteBuffer.wrap(str.getBytes()); # outputChannel.write(bb);
与不使用FileChannel的具体区别么?

-dongtalk@gmail.com

没有区别,只是不同的API而已,你可以直接使用stream,但FileChannel
提供了更多的控制和功能,支持并发和内存映射,上述代码只是演示了怎么
获得channel,然后写数据,无法体现其他功能。

热点排行