import java.io.*;
public class readFile{
  public static void main(String args[]){
    int a;
    FileInputStream fis;
    try{
      fis=new FileInputStream(args[0]);//为什么要args[0]这个参数?
    }catch(FileNotFoundException e){
      System.out.println("File not found!");
      return; //什么作用呀?
    }catch(ArrayIndexOutOfBoundsException e){//是只要出现数组就要写数组出界异常吗?
      System.out.println("Usage:readFile FileName");
      return;
    }
     try{
     do{
       a=fis.read();
       if(a!=-1)System.out.println((char)a);//a!=-1什么含义是输入enter键的意思吗?
     }while(a!=-1);
       fis.close();//不关闭文件会有什么影响呢?
}catch(IOException e){
System.out.println("IO异常");
}
  }
}

解决方案 »

  1.   

    import java.io.*;
    public class readFile{
      public static void main(String args[]){
        int a;
        FileInputStream fis;
        try{
          fis=new FileInputStream(args[0]);//为什么要args[0]这个参数?A:args[0]是运行程序是船进的参数
        }catch(FileNotFoundException e){
          System.out.println("File not found!");
          return; //什么作用呀?A:捕捉到该异常就返回,不再进行下面的异常捕捉。
        }catch(ArrayIndexOutOfBoundsException e){//是只要出现数组就要写数组出界异常吗?A:应该是。但似乎这个错误系统会捕捉,不用写出来。
          System.out.println("Usage:readFile FileName");
          return;
        }
         try{
         do{
           a=fis.read();
           if(a!=-1)System.out.println((char)a);//a!=-1什么含义是输入enter键的意思吗?A:a相当于fis不断读出的字符,如果为-1,大概就是文件结束。
         }while(a!=-1);
           fis.close();//不关闭文件会有什么影响呢?A:如果不关闭,在其他地方要再打开这个文件会出问题。
    }catch(IOException e){
    System.out.println("IO异常");
    }
      }
    }
      

  2.   

    fis=new FileInputStream(args[0]);//为什么要args[0]这个参数?  这是在命令行参数中指明一个文件名,然后打开该文件.catch(ArrayIndexOutOfBoundsException e){//是只要出现数组就要写数组出界异常吗?  这个地方不合适,不应该用异常来捕获用户命令行输入的错误,应该自己检测args参数是否符合要求,而不是利用异常,不是好习惯.其他的不说了,楼上说了.
      

  3.   

    同意楼上的关于数组越界异常,thinking in java里说得很明白,一般的越界是可以避免的,只要加以合适的判断,而不是抛异常,而且抛异常更耗时间