错误:FileOutputStream 可能未初始化

发布于 2024-09-26 15:22:20 字数 1142 浏览 0 评论 0原文

我试图在 onCreate 方法中运行这段代码,作为写入私有数据供我的应用程序使用的初始测试。此代码直接来自 Android SDK 开发指南,位于 此处< /a>

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

但是,这段代码给了我底部 3 行代码的错误。该错误是未处理的异常。建议的快速修复方法是执行以下操作:

    String FILENAME = "hello_file";
    String string = "hello world!";

    FileOutputStream fos;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fos.write(string.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是执行此操作后,我在底部两行中收到错误,指出 fos 可能未初始化。我该如何修复此代码?

I'm trying to run this piece of code inside my onCreate method as an initial test into writing private data for my app to use. This code is straight out of the Android SDK development guide located here

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

However, this code give me errors for the 3 lines of code at the bottom. The error is an unhandled exception. The suggested quick fix is do to the following:

    String FILENAME = "hello_file";
    String string = "hello world!";

    FileOutputStream fos;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fos.write(string.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But after doing that I get an error for the bottom two lines which states that fos may not be initialized. How can I fix this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三五鸿雁 2024-10-03 15:22:21

替换

FileOutputStream fos;

FileOutputStream fos = null;

Replace

FileOutputStream fos;

with

FileOutputStream fos = null;
森末i 2024-10-03 15:22:21

是的,这里的问题是,如果您收到 FileNotFoundException,您会尝试打印异常并继续,但在这种情况下,fos 变量将永远不会被分配值,因为“openFileOutput”调用不会完成。这很好,因为在您无法打开文件的情况下,您不想继续尝试写入未打开的文件。

由于 FileNotFoundException 是 IOException,因此您可以将所有这些简化为:

String FILENAME = "hello_file";
String string = "hello world!";

try {
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

在这种情况下,引发异常的第一件事会导致打印堆栈跟踪,并且您可以跳过 try {} 块其余部分中的任何后续操作。

水泥的答案的问题在于,尽管它是由编译器错误得到的,但如果第一个块抛出异常,第二个块会给你一个 NullPointerException 。

Yes, the problem here is that if you get a FileNotFoundException you try to just print out the exception and continue, but in that case, the fos variable would have never been assigned a value, since the "openFileOutput" call wouldn't have completed. This is fine, because in the case when you weren't able to open a file, you don't want to continue to try to write to the file you didn't open.

Since a FileNotFoundException IS a IOException, you could simplify all of this as:

String FILENAME = "hello_file";
String string = "hello world!";

try {
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

In this case, the first thing that raises an exception causes the stack trace to get printed, and you skip any subsequent operations in the rest of the try {} block.

The problem with cement's answer is that, although it gets by the compiler error, if the first block ever throws the exception, the second block will give you a NullPointerException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文