Topic: 帮小弟看看这段代码怎么了?( unreported exception java.io.FileNotFoundException)

  Print this page

1.帮小弟看看这段代码怎么了?( unreported exception java.io.FileNotFoundException) Copy to clipboard
Posted by: cjy
Posted on: 2004-09-14 14:36

各位大侠,帮小弟看看这段代码怎么了?

import java.util.*;
import java.io.*;
class CFileExample2
{
private String s="";
private RandomAccessFile f1=new RandomAccessFile("file1.txt","rw");
private RandomAccessFile f2=new RandomAccessFile("file2.txt","rw");
void fileLink()
{
  s=f1.readLine()+f2.readLine();
  char c[]=s.toCharArray();
  Arrays.sort(c);
  FileOutputStream out=new FileOutputStream("new.txt");
  for(int i=0;i<c.length;i++)
  {
   out.write(c[i]);
  }
  out.close();
  f1.close();
  f2.close();
}
}
public class FileExample2
{
public static void main(String[] args)throws IOException
  {
  CFileExample2 f=new CFileExample2();
  f.fileLink();
  }
}
FileExample2.java:6: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
private RandomAccessFile f1=new RandomAccessFile("file1.txt","rw");
^
FileExample2.java:7: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
private RandomAccessFile f2=new RandomAccessFile("file2.txt","rw");
^

我的file1.txt各file2.txt文件都在同一文件夹下,并且每个文件只有一行字母而已。我觉得我的声明没有错的嘛?why?

2.Re:各位大侠,帮小弟看看这段代码怎么了? [Re: cjy] Copy to clipboard
Posted by: 爱如泣血
Posted on: 2004-09-14 17:15

读写文件应该用异常处理,把这个
throws IOException
加在方法后,{号之前.

3.Re:各位大侠,帮小弟看看这段代码怎么了? [Re: cjy] Copy to clipboard
Posted by: augxiao
Posted on: 2004-09-14 17:30

应该这样声明:
private RandomAccessFile f1;
private RandomAccessFile f2;
然后在fileLink()中
f1=new RandomAccessFile("file1.txt","rw");
f2=new RandomAccessFile("file2.txt","rw");

如果要象你那样定义,应该把他们放在fileLink()中,而不是外面;

并且还要处理FileNotFoundException和IOException

4.Re:各位大侠,帮小弟看看这段代码怎么了? [Re: cjy] Copy to clipboard
Posted by: Jawen
Posted on: 2004-09-14 17:49

爲什麽要這樣子阿?望請教!

5.Re:各位大侠,帮小弟看看这段代码怎么了? [Re: cjy] Copy to clipboard
Posted by: rebirth
Posted on: 2004-09-14 18:07

因为做下面这步操作
private RandomAccessFile f1=new RandomAccessFile("file1.txt","rw");
会发生
java.io.FileNotFoundException 例外, 即找不到该文件的例外,
所以必须把new RandomAccessFile("file1.txt","rw");
放在try{}catch(...){}里面.

一般都是先声明再定义. 如下:
private RandomAccessFile f1=null;
public function(){
try{
f1=new RandomAccessFile("file1.txt","rw");
}catch(Exception e){
//抛出例外.
}
}

6.Re:各位大侠,帮小弟看看这段代码怎么了? [Re: cjy] Copy to clipboard
Posted by: kjjx
Posted on: 2004-09-14 18:41

提示你一下,对文件操作必须捕捉有关异常!

7.Re:帮小弟看看这段代码怎么了 [Re: cjy] Copy to clipboard
Posted by: cjy
Posted on: 2004-09-16 15:33

各位老大,不是java有两种抛出异常的方式吗?我在main中用throw IOException直接由方法抛出异常,也应该没错呀?但正如上面各位老大所说,用了try…catch块来捕捉异常,程序就编译成功了。但是为什么会这样呢?由方法抛出异常和在方法内编写try…catch来捕捉异常有什么不同吗?望各位老大能告知。小弟在这里谢过了。

8.Re:帮小弟看看这段代码怎么了 [Re: cjy] Copy to clipboard
Posted by: siage
Posted on: 2004-09-16 15:53

去看看j2se sdk doc,里面每个方法都有相应的异常,到时候照抄上就可以了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923