cantiaozi
2017-03-24 01:22
采纳率: 50%
浏览 1.3k
已采纳

编了个java小程序,关于FileInputStream流的,但编译的时候显示变量找不到,求教!

import java.io.*;
public class TestFileInputStream {
public static void main(String []args) {
try {
FileInputStream stream =null;
stream = new FileInputStream("E:/javadocument/Stream/TestFileInputStream.java");
} catch (FileNotFoundException e) {
System.out.println("wenjianmingmeizhaodao");
System.exit(-1);
}

  long num = 0;
  int b; 
  try {
     while((b = stream.read()) != -1) {
        System.out.print((char)b);
        num++; 
     }
     stream.close();
     System.out.println();
     System.out.println(num);
  } catch (IOException e) {
     System.out.println("fashengcuowu");
     System.exit(-1);
  }

}
}

编译时总是显示变量stream找不到符号,为什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • zhang_zhongkang 2017-03-24 01:27
    已采纳

    你的变量定义在 try 里边了是局部变量了 下边当然取不到了

    点赞 打赏 评论
  • 夜幕下的尖椒 2017-03-26 07:41

    import java.io.*;
    public class TestFileInputStream {
    public static void main(String []args) {
    FileInputStream stream =null;
    try {
    stream = new FileInputStream("E:/javadocument/Stream/TestFileInputStream.java");
    } catch (FileNotFoundException e) {
    System.out.println("wenjianmingmeizhaodao");
    System.exit(-1);
    }
    long num = 0;
    int b;
    try {
    while((b = stream.read()) != -1) {
    System.out.print((char)b);
    num++;
    }
    stream.close();
    System.out.println();
    System.out.println(num);
    } catch (IOException e) {
    System.out.println("fashengcuowu");
    System.exit(-1);
    }
    }
    }

    这样写 就行了

    点赞 打赏 评论

相关推荐 更多相似问题