本人初学JAVA,想用到 FileInputStream 读取文件时遇到了问题,程序段如下:import java.io.*;public class HelloWord {    public static void main(String[] args) {        String strFileName1="e:\\pdf\\file01.txt";  // 文件名,保证是正确的
        File objFile = new File(strFileName1);        System.out.println("File name: " + strFileName1);  //显示的文件名是正确的
        System.out.println("File Exists: " + objFile.exists()); //显示 TRUE
        System.out.println("Can Read:" + objFile.canRead());    //显示 TRUE

        FileInputStream is = new FileInputStream(objFile);  //这里有问题,编译时就已报“Unhandled exception type FileNotFoundException”         FileInputStream is = new FileInputStream(strFileName1);  //直接用文件名也有问题,编译时也报上面一行同样的错。
    }
}请教这是什么问题?我看别我的例子中,也是这么用的呀?谢谢了!

解决方案 »

  1.   

    你没有捕获异常,当你用 FileInputStream is = new FileInputStream(objFile); 的时候可能不存在objFile这个文件,所以java要求你try{}catch{}或者throws Exception来处理这个异常或者声明这个异常
    改成这个就可以了
    import java.io.*;class HelloWord {    public static void main(String[] args) {        String strFileName1="e:\\pdf\\file01.txt";  // 文件名,保证是正确的
            File objFile = new File(strFileName1);        System.out.println("File name: " + strFileName1);  //显示的文件名是正确的
            System.out.println("File Exists: " + objFile.exists()); //显示 TRUE
            System.out.println("Can Read:" + objFile.canRead());    //显示 TRUE

    try
    {
     FileInputStream is = new FileInputStream(objFile);  //这里有问题,编译时就已报“Unhandled exception type FileNotFoundException” 
    }
            catch(Exception e)
            {
            e.printStackTrace();
            }       // FileInputStream is = new FileInputStream(strFileName1);  //直接用文件名也有问题,编译时也报上面一行同样的错。
        }
    }
      

  2.   

    Unhandled exception type FileNotFoundException
    未捕获(处理) FileNotFoundException 异常
      

  3.   

    要try{...} catch(IOException e)