源代码在这里:
import java.io.*;public class TestFileInputStream { public static void main(String[] args) throws IOException {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("e:/java/1.txt");
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件!");
System.exit(0);
e.printStackTrace();
}
long num = 0;
while((b=in.read()) !=-1){
System.out.print((char)b);
num++;
} }}
请教一下,为什么如果事先不声明 in,也就是说直接在try里面声明FileInputStream in = new FileInputStream("xx")
的话,下面在  while里面会找不到in?

解决方案 »

  1.   

    也就是说写成如下格式的话就有问题,是为什么?
    import java.io.*;public class TestFileInputStream { public static void main(String[] args) throws IOException {
    int b = 0;
    try {
    FileInputStream in = new FileInputStream("e:/java/1.txt");
    } catch (FileNotFoundException e) {
    System.out.println("找不到指定文件!");
    System.exit(0);
    e.printStackTrace();
    }
    long num = 0;
    while((b=in.read()) !=-1){
    System.out.print((char)b);
    num++;
    } }}
      

  2.   

    在try..catch中定义的变量就是一个只属于try..catch中的一个局部变量
    try..catch处的自然不能访问咯。。
      

  3.   

    上面都是正解作用域问题。。在try{}里面定义in,in只能在try里面可见可用。。
      

  4.   

    try catch 也算作用域啊  学习了谢谢楼上各位