2

我是 java 新手,我正在尝试编写这段代码,但是在使用我的变量时它以某种方式将其视为错误。已被宣布为 ofc。

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();
                }
            }
        }
}
4

6 回答 6

3

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

    ObjectInputStream inputStream = null;
    try{
     inputStream = new ObjectInputStream(new  FileInputStream("countries"));
     ........
 }
于 2012-10-23T21:24:10.697 回答
1

您在块的范围内inputStream 定义try,因此无法在外部访问它。

您可以通过执行以下操作来解决此问题,

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

即在-block之外定义变量try并在其中分配它。这样,您可以在-blockinputStream之外访问。try

于 2012-10-23T21:24:26.130 回答
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();
    }
}

或者甚至更好地使用try-with-resources(嘿,Java 7 不再新鲜!)

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

不需要finally,close()等。

于 2012-10-23T21:24:45.137 回答
1

这是变量范围的问题。

您的inputStream变量属于try不包含与 or 块相同范围的catch块,finally导致在or块inputStream中未声明。因此,该变量在块中是未知的,解释了您的 IDE 显示的“变量可能未初始化”。catchfinallycatch

简单地说,将您的变量初始化到null第一个try/之外catch,如下所示:

ObjectInputStream inputStream = null;
try{
于 2012-10-23T21:26:55.397 回答
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();
            }
        }
    }
于 2012-10-23T21:24:21.210 回答
0

inputstream 在您的 try/catch 的 {} 之外不存在

于 2012-10-23T21:24:26.937 回答