import java.io.*;
public class TextEx{
public static void main (String [] args){
try{
new TextEx().f2();
}catch(IOException e){
e.printStackTrace();
}
FileInputStream in= null;
try{
in=new FileInputStream("myFile.txt");
int b;
b=in.read();
while(b!=-1)
{System.out.println((char)b);
b=in.read();
}
}catch(IOException e){
System.out.println(e.getMessage());
}catch(FileNotFoundException e){
e.printStackTrace(); 
}finally{
try{
in.close();
}catch(IOException e){
e.printStackTrace(); 
}
}
}


void m(int i) throws ArithmeticException{
if(i==0)
throw new ArithmeticException("被除数为0");
}
void f() throws FileNotFoundException,IOException{
FileInputStream in=new FileInputStream("myfile.txt");
int b;
b=in.read();
while(b!=-1){
System.out.println((char) b);
b=in.read();
}
}
void f2() throws IOException{
f();
}
}







解决方案 »

  1.   

    你的当前目录下就没有myFile.txt 这个文件吧
      

  2.   

    如果你使用相对路径  那与你的class文件(注意:不是java文件,而且该class文件不能在压缩包里面)顶级包平级的目录下面应该有 myfile.txt 文件! 否则你是找不到的!  、使用相对路径时要麻烦一点!在你的in=new FileInputStream("myFile.txt"); 语句之前你可以自己调试一下  打印出当前路径看下:System.out.println(new File("").getAbsolutePath());
      

  3.   

    如果你确定你的File.txt会一直跟随你的当前目录的话,用相对路径较好,如果不是,就用绝对路径来指向,否则,就会抛出异常罗,因为当前路径下找不到你的文件嘛。
      

  4.   

    如果你一定要捕捉到这两个异常的话,请你把子类异常放在父类异常的前面,这是一定的。} catch (FileNotFoundException e) { //继承自IOException 
           e.printStackTrace();
    }catch (IOException e) {
    System.out.println(e.getMessage());
    }如果父类异常放在最前面的话,即使是FileNotFoundException 错误,但是已经被IOException 的catch捕捉
    那么catch FileNotFoundException 再去处理当然会说已经被处理