2012-10-23 56 views
2

我是新来的Java和我试图写这段代码,但不知何故,它认为它是一个错误,当使用我的变量。已被宣布为c。代码突出显示为语法错误。为什么?

import java.io.*; 

public class FileRead { 
    public void readCountries(String file){ 
     try{ 
      ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("countries")); 
      Object obj = null; 
      while ((obj = inputStream.readObject()) != null) { 
       if (obj instanceof Country) { 
        System.out.println(((Country)obj).toString()); 
       } 
      } 
     } catch (EOFException ex) { //This exception will be caught when EOF is reached 
       System.out.println("End of file reached."); 
      } catch (ClassNotFoundException ex) { 
       ex.printStackTrace(); 
      } catch (FileNotFoundException ex) { 
       ex.printStackTrace(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } finally { 
       //Close the ObjectInputStream 
       try { 
        if (inputStream != null) { //////////ERROR: inputStream cannot be resolved to a variable 
         inputStream.close(); //////////// Same here 
        } 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
} 

回答

3

将您的inputStream声明移到try块之外。如果您在try中定义,则在try块之外不可见。

ObjectInputStream inputStream = null; 
    try{ 
    inputStream = new ObjectInputStream(new FileInputStream("countries")); 
    ........ 
} 
+0

TY为explanatios。 –

0

变量声明的try { ... }块内,因此它的范围和可见性受到限制它。你不能在外面使用它。

可以将块之外声明它:

ObjectInputStream inputStream = null; 
try{ 
     inputStream = new ObjectInputStream(new FileInputStream("countries")); 
    //...  
} catch (EOFException ex) { //This exception will be caught when EOF is reached 
    //...  
} finally { 
      //Close the ObjectInputStream 
      try { 
       if (inputStream != null) { //////////ERROR: inputStream cannot be resolved to a variable 
        inputStream.close(); //////////// Same here 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 
1

你定义try块的inputStream内部的范围,并且因此不能外面访问它。

你可以做这样的事情解决这个问题,

ObjectInputStream inputStream = null; 
try{ 
    inputStream = new ObjectInputStream(new FileInputStream("countries")); 
    ... 
} 

即定义变量try - 阻塞的和内分配它。通过这种方式,您可以在try区块之外访问inputStream

0

的InputStream不存在外的{}你的try/catch

1

的这很简单,你是在试图访问它被宣布为范围之外的变量。这里是你的问题的一个简单的例子:

try { 
    int i = 0; 
} catch(Exception e) { 
    //... 
} 

++i; 

你看?一旦变量在声明时脱离大括号,它就会丢失。在您的例子:

try{ 
    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("countries")); 
    //... 
} finally { 
    if (inputStream != null) { //////////ERROR: inputStream cannot be resolved to a variable 
     inputStream.close(); //////////// Same here 
    } 
} 

只需将inputStream外:

ObjectInputStream inputStream = null; 
try{ 
    inputStream = new ObjectInputStream(new FileInputStream("countries")); 
    //... 
} finally { 
    if (inputStream != null) { 
     inputStream.close(); 
    } 
} 

甚至更​​好使用试穿与资源(嘿,Java 7的不再是新的,新鲜!)

try(ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("countries")) { 
    //... 
} 

finally,close()等需要。

1

这是一个可变范围的问题。

inputStream你可变属于try块不包含同一范围作为catchfinally块引起inputStreamcatchfinally块被宣告。因此,该变量在catch块内未知,说明您的IDE显示的“变量可能未初始化为”。

简单地说,你的初始化变量null第一try/catch外面如下:

ObjectInputStream inputStream = null; 
try{