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属しているため、またはブロックで宣言されていません。したがって、この変数はブロック内では不明であり、IDE によって表示される「変数が初期化されていない可能性がある」ことを説明しています。trycatchfinallyinputStreamcatchfinallycatch

次のように、変数を最初の/nullの外側に初期化するだけです。trycatch

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

入力ストリームは、try/catch の {} の外に存在しません

于 2012-10-23T21:24:26.937 に答える