这个程序里面的RandomAccessFile readWrite=new RandomAccessFile("rand.dat","rw");为什么必须写在try里面

yuyistar 2008-03-24 09:57:56
package random;

import java.io.RandomAccessFile;
import java.util.LinkedList;

public class RandomFile {
public RandomFile() {
}
void show(){

try{
int temp[]={12,31,56,23,27,1,43,65,4,99};
RandomAccessFile readWrite=new RandomAccessFile("rand.dat","rw");
for(int i=0;i<temp.length;i++){
readWrite.writeInt(temp[i]);
}
System.out.println("逆向后为:");
for(int i=temp.length-1;i>=0;i--){
readWrite.seek(i*4);
System.out.print(readWrite.readInt());
if(i>0){
System.out.print(",");
}
}
System.out.println();
}
catch(Exception ee){
System.out.println("异常:"+ee);
}
finally{
System.out.println("始终执行");
System.out.println("在finally中执行");
}
}

public static void main(String[] args) {
RandomFile randomfile = new RandomFile();
randomfile.show();
}
}
...全文
126 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueblueorange 2008-03-25
  • 打赏
  • 举报
回复
因为RandomAccessFile可能会抛出非运行时异常,非运行时异常必须捕捉
eric0cn 2008-03-24
  • 打赏
  • 举报
回复
看看这个类的声明:
public RandomAccessFile(java.lang.String arg0, java.lang.String arg1) throws java.io.FileNotFoundException;

它声明会可能会抛出FileNotFoundException,所以你必须显式捕获
ql103 2008-03-24
  • 打赏
  • 举报
回复
好象是你初始化RandomAccessFile的对象时候会抛出异常。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧