NIO
共 4 篇文章
从ByteBuffer中获取基本类型
我们可以从ByteBuffer中产生出不同类型值的方法 public class Test{ public static final int BSIZE=1024; public static void main() { ByteBuffer bb=ByteBuffer.allocate(BSIZE); int i=0; while(i++<bb.limit()) if(bb.get()!=0) System.out.print("nozero"); System.out.println("i ="+i); bb.rewind(); bb.asShortBuffer().put((short…
2012-03-04 14:03 · NIO · bytebuffer
NIO基础知识2
asCharBuffer()一次可以输出缓冲区中信息。使用方法为: ByteBuffer buffer=ByteBuffer.allocate(1024); System.out.println(buffer.asCharBuffer()); //**这个地方注意:**普通缓冲器容纳的是普通字节,假如把它们转换成字符,要在输入他们的时候进行编码,代码如下: public class BufferToText { private static final int BSIZE=1024; public static void main(String[] args) throws Exceptio…
2011-11-20 23:00 · NIO · bytebuffer · asCharBuffer
transferTo和transferFrom
transferTo和transferFrom允许我们将一个通道和另一个通道直接相连,这样很容易实现文件拷贝的功能,如下代码: String[] arr=new String[]{"D:\\data.txt","D:\\data2.txt"}; FileChannel in=new FileInputStream(arr[0]).getChannel(), out =new FileOutputStream(arr[1]).getChannel(); //将一个通道和另一个通道直接相连接 in.transferTo(0, in.size(), out);
2011-11-20 22:01 · NIO · FileChannel · transferto · transferfrom
Java NIO基础知识
java NIO比普通IO访问速度有较大的提高,原因是新IO所使用的结构接近于操作系统执行IO的方式:通道和缓冲器。唯一直接与通道交互的缓冲期是ByteBuffer. 参考下面的例子: public class GetChannel { private static final int BSIZE=1024; public static void main(String[] args) throws Exception{ FileChannel fc=new FileOutputStream("D:\\data.txt").getChannel(); fc.write(ByteBuffer.…
2011-11-20 21:47 · NIO · nio · bytebuffer · getchannel