chf是前文的一个FileChannel ByteBuffer buf = ByteBuffer.allocate(50);
chf.read(buf);
buf.flip();
byte [] byt = new byte[100];
buf.get(byt);
然后就出来了 :
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.HeapByteBuffer.get(Unknown Source)
at java.nio.ByteBuffer.get(Unknown Source)
at Sample.main(Sample.java:22)请问这怎么办
chf.read(buf);
buf.flip();
byte [] byt = new byte[100];
buf.get(byt);
然后就出来了 :
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.HeapByteBuffer.get(Unknown Source)
at java.nio.ByteBuffer.get(Unknown Source)
at Sample.main(Sample.java:22)请问这怎么办
解决方案 »
- java 字符串查找用ArrayList还是Hashtable?
- 谁知道 FileDescriptor 的作用?
- icon 问题
- 分享下
- 请高手帮看看这个xmlhttp的问题.
- comparable 和comparator 的返回值问题~~
- 如何在JAVA里调用windows动态库dll!
- 如何不要让TextField中的内容不被覆盖
- 在一个List中放若干个Map,怎么穷举出来所有的Map的value的组合
- 怎么用Applet获得当前页面的环境变量??
- 写了个java http多线程下载的程序, 发现有的网站图片可以下载,有的网站图片不可以下载, 为什么?
- 程序打成jar包后,如何使用log4j.properties文件?
不管谁大谁小 都出那个异常
chf.read(buf); //这句话抛的异常
buf.flip();
byte [] byt = new byte[100];
buf.get(byt);参考如下代码 public static void main(String[] args) throws IOException{
FileChannel channel = new FileOutputStream("D:/a.txt").getChannel();
// 字节方式写入
channel.write(ByteBuffer.wrap("hello, NIO world in java!".getBytes()));
channel.close();
// 根据FileInputStream获得通道FileChannel
channel = new FileInputStream("D:/a.txt").getChannel();
// ByteBuffer分配空间,16个字节
// 这里需要知道 byte是1字节, short和char是2字节,int和float是4字节
// long和double是8字节 1byte=8bit 。 基本只是还是必须记住的。
ByteBuffer buff = ByteBuffer.allocate(16);
// 字节数组数据装入buff,
channel.read(buff);
// 反转此缓冲区
buff.flip();
byte [] byt = new byte[10];
System.out.println(buff.get(byt)); // 根据FileOutputStream获得通道FileChannel
}
FileChannel channel = new FileOutputStream("D:/a.txt").getChannel();
// 字节方式写入
channel.write(ByteBuffer.wrap("hello, NIO world in java!".getBytes()));
channel.close();
// 根据FileInputStream获得通道FileChannel
channel = new FileInputStream("D:/a.txt").getChannel();
// ByteBuffer分配空间,16个字节
// 这里需要知道 byte是1字节, short和char是2字节,int和float是4字节
// long和double是8字节 1byte=8bit 。 基本只是还是必须记住的。
ByteBuffer buff = ByteBuffer.allocate(16);
// 字节数组数据装入buff,
channel.read(buff);
// 反转此缓冲区
buff.flip();
byte [] byt = new byte[10];
System.out.println(buff.get(byt));
channel.close();
}