java 如何在 Try/Catch 块之前初始化 InputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28594252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:52:18  来源:igfitidea点击:

How to Initialize an InputStream Before Try/Catch Block

javainitializationinputstream

提问by Malonge

I need to get a file name string, and try to open the file. If the file is not found, I loop until a proper string is entered.

我需要获取一个文件名字符串,然后尝试打开该文件。如果找不到文件,我会循环直到输入正确的字符串。

public static void main(String[] args){

// Get file string until valid input is entered.
System.out.println("Enter file name.\n Enter ';' to exit.");
String fileName = sc.nextLine();
boolean fileLoop = true;
InputStream inFile;

while (fileLoop){
    try{
        inFile = new FileInputStream(fileName);
        fileLoop = false;
    } catch (FileNotFoundException e) {
        System.out.println("That file was not found.\n Please re enter file name.\n Enter ';' to exit.");
        fileName = sc.nextLine();
        if (fileName.equals(";")){
            return;
        }
   } 

}

// ****** This is where the error is. It says inFile may not have been initalized. ***
exampleMethod(inFile);
}

public static void exampleMethod(InputStream inFile){

    // Do stuff with the file.
}

When I try to call exampleMethod(inFile) NetBeans tells me that the InputStream inFile may not have been initialized. I assume this is because the assignment is within a try catch block. As one can see, I tried declaring the object outside of the loop and that did not work.

当我尝试调用 exampleMethod(inFile) 时,NetBeans 告诉我 InputStream inFile 可能尚未初始化。我认为这是因为分配在 try catch 块内。如您所见,我尝试在循环之外声明对象,但没有奏效。

I also tried initializing the input stream outside of the loop with the following:

我还尝试使用以下方法在循环外初始化输入流:

InputStream inFile = new FileInptStream();
// This yeilds an eror because there are no arguments.

and this as well:

这也是:

InputStream inFile = new InputStream();
// This doesn't work because InputStream is abstract.

How do I ensure that I initalize this InputStream while still allowing for looping until valid input is entered?

我如何确保我初始化这个 InputStream 同时仍然允许循环直到输入有效输入?

Thank You

谢谢

回答by Daniel Kaplan

To fix this, change this line of code:

要解决此问题,请更改以下代码行:

InputStream inFile;

to this:

对此:

InputStream inFile = null;

The reason you have to do this is because Java prevents you from using local variables that are uninitialized. Using an uninitialized variable is often an oversight, so Java prevents it from being allowed in this scenario. As @immibis pointed out, this variable will always be initialized, but the compiler isn't smart enough to figure it out in this case.

您必须这样做的原因是因为 Java 阻止您使用未初始化的局部变量。使用未初始化的变量通常是一种疏忽,因此 Java 禁止在这种情况下使用它。正如@immibis 指出的那样,这个变量将始终被初始化,但编译器不够聪明,无法在这种情况下弄清楚。