A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王海龙 中级黑马   /  2012-12-22 13:07  /  2197 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class ProductManager {
  2.     public void save(File f) {
  3.         try {
  4.             ObjectOutputStream oos = new ObjectOutputStream(
  5.                     new FileOutputStream(f));
  6.             Scanner input = new Scanner(System.in);

  7.             System.out.println("Please input prodcutID");
  8.             String id = input.next();
  9.             System.out.println("Please input prodcut name:");
  10.             String name = input.nextLine();
  11.             System.out.println("Please input the price:");
  12.             double price = input.nextDouble();
  13.             System.out.println("Please input the nunmer:");
  14.             int count = input.nextInt();
  15.             Product p = new Product(id, name, count, price);
  16.             oos.writeObject(p);
  17.             

  18.         } catch (FileNotFoundException e) {
  19.             // TODO Auto-generated catch block
  20.             e.printStackTrace();
  21.         } catch (IOException e) {
  22.             // TODO Auto-generated catch block
  23.             e.printStackTrace();

  24. //下面这句代码中,提示无法解析oos,oos不是就在try里面定义的吗?为什么?
  25.         } finally{
  26.             oos.close();
  27.         }

  28.     }

  29. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

6 个回复

倒序浏览
那是因为在不同的代码块,在try代码块中定义的变量,在其它代码块中访问不到,
要把try {

            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));

改为ObjectOutputStream oos=null;
        try {
             oos = new ObjectOutputStream(
                    new FileOutputStream(f));

即可。

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
楼主请看这里来!看我改正的部分,因为代码块里定义的变量只在本代码快有效!
所以,改成下面颜色的部分!

public class ProductManager {
    public void save(File f) {
         ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(f));
            Scanner input = new Scanner(System.in);

            System.out.println("Please input prodcutID");
            String id = input.next();
            System.out.println("Please input prodcut name:");
            String name = input.nextLine();
            System.out.println("Please input the price:");
            double price = input.nextDouble();
            System.out.println("Please input the nunmer:");
            int count = input.nextInt();
            Product p = new Product(id, name, count, price);
            oos.writeObject(p);
            

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

//下面这句代码中,提示无法解析oos,oos不是就在try里面定义的吗?为什么?
        } finally{
            oos.close();
        }

    }

}

评分

参与人数 1技术分 +1 收起 理由
邵天强 + 1

查看全部评分

回复 使用道具 举报
oos应该在try外面定义,try定义了,其他语句块里访问不到
  1. ObjectOutputStream oos = null;
  2.    try {
  3.                 oos = new ObjectOutputStream(
  4.                 new FileOutputStream(f));
复制代码
还有啊,关闭时应该判断一下是否为空
  1. try {
  2.                 if(oos!=null){
  3.                         cos.close();
  4.                 }
  5.         }catch(IOException e){
  6.                  e.printStackTrace();

  7.         }
复制代码
回复 使用道具 举报
定义在try里面是try局部变量,finally块当然访问不到try里面的变量了。
回复 使用道具 举报
你在try里面定义的那个oos,只能在try内访问,每个变量都有自己作用域范围,要将oos放到try外面定义

回复 使用道具 举报
在try中定义属于局部变量!要放到外面比较好些!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马