急!!!JAVA Bug!
http://www.diybl.com/ 2007-12-6 网络 点击:
[ 评论 ]
文章搜索:
【点击打包该文章】
public void testMappedByteBufferRead() {
int length = 10485760;
MappedByteBuffer out = null;
RandomAccessFile ranFile = null;
File file = null;
try {
file = new File("d:/test.txt");
ranFile = new RandomAccessFile(file, "rw");
out = ranFile.getChannel().map(
FileChannel.MapMode.READ_WRITE, 0, length);
for (int i = 0; i < length; i++)
out.put((byte) ''x'');
System.out.println("Finished writing");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
file.delete();
System.out.println("Delete File");
Thread.sleep(100000);
} catch (Exception e) {
e.printStackTrace();
}
}
上面的操作都会正常的完成,除了最后一步:文件无法删除!即使你通过资源管理器直接强制删除也不行,说"文件正在使用",也就是说out无法关闭!
在网上收集一些资料都说是JAVA的一个BUG,一些网友自己写out的close()函数:
用到NIO的相关类--MappedByteBuffer时的一个BUG。
例如:
public void testMappedByteBufferRead() {
int length = 10485760;
MappedByteBuffer out = null;
RandomAccessFile ranFile = null;
File file = null;
try {
file = new File("d:/test.txt");
ranFile = new RandomAccessFile(file, "rw");
out = ranFile.getChannel().map(
FileChannel.MapMode.READ_WRITE, 0, length);
for (int i = 0; i < length; i++)
out.put((byte) ''x'');
System.out.println("Finished writing");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
file.delete();
System.out.println("Delete File");
Thread.sleep(100000);
} catch (Exception e) {
e.printStackTrace();
}
}
上面的操作都会正常的完成,除了最后一步:文件无法删除!即使你通过资源管理器直接强制删除也不行,说"文件正在使用",也就是说out无法关闭!
在网上收集一些资料都说是JAVA的一个BUG,一些网友自己写out的close()函数:
public static void clean(final Object buffer) throws Exception {
AccessController.doPrivileged(new PrivilegedAction() {
public Object&n[1] [2]
如果图片或页面不能正常显示请点击这里 站内搜索:
推荐文章 |
