RandomAccessFile 声明后无法被编译器找到

发布于 2024-11-04 13:20:22 字数 439 浏览 3 评论 0原文

当我尝试编译下面的代码时,会产生以下错误:

找不到符号
符号:变量飞机文件

该错误是由最后一条语句产生的。

为什么RandomAccessFile对象声明后找不到?

谢谢!

public static void main(String[] args)
{

    try
    {
        RandomAccessFile airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw");
    }
    catch (FileNotFoundException fnfe)
    {
        fnfe.printStackTrace();
    }

    airplanesFile.writeUTF("Test");
}

The code below produces the following error when I try to compile it:

cannot find symbol
symbol : variable airplanesFile

The error is produced by the last statement.

Why can the RandomAccessFile object not be found after it's declared?

Thanks!

public static void main(String[] args)
{

    try
    {
        RandomAccessFile airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw");
    }
    catch (FileNotFoundException fnfe)
    {
        fnfe.printStackTrace();
    }

    airplanesFile.writeUTF("Test");
}

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

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

发布评论

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

评论(5

轻许诺言 2024-11-11 13:20:22

这与变量作用域有关。 airplanesFile 在 try 块的大括号内声明。当编译器命中 try 块的右大括号时,它就会超出范围。

在 try 语句之前声明 RandomAccessFileplanesFile = null;,并更改 RandomAccessFileplanesFile = new RandomAccessFile("airplanesFile.ran", "rw");
airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw"); 并且您的问题应该消失。

This is to do with variable scoping. airplanesFile is declared within the braces of the try block. It goes out of scope when the compiler hits the closing brace of the try block.

Declare RandomAccessFile airplanesFile = null; before the try statement, and alter RandomAccessFile airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw");
to airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw"); and your problem should go away.

此刻的回忆 2024-11-11 13:20:22

因为planesFile只在try块中有效。
试试这个:

public static void main(String[] args)
{
    RandomAccessFile airplanesFile = null;

    try
    {
         airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw");
    }
    catch (FileNotFoundException fnfe)
    {
        fnfe.printStackTrace();
    }

    try {
        airplanesFile.writeUTF("Test");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Because airplanesFile is only valid in try block.
Try this:

public static void main(String[] args)
{
    RandomAccessFile airplanesFile = null;

    try
    {
         airplanesFile = new RandomAccessFile("airplanesFile.ran", "rw");
    }
    catch (FileNotFoundException fnfe)
    {
        fnfe.printStackTrace();
    }

    try {
        airplanesFile.writeUTF("Test");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
花桑 2024-11-11 13:20:22

这超出了范围。 try catch 包含声明。

如果变量/对象是在代码块内任何 { } 内声明的,则不能在其外部使用它。 在你的情况下,你必须

airplanesFile.writeUTF("Test");

在 try catch 内执行...。

It's out of scope. The try catch encloses the declaration.

If a variable/object is declared within a code block, inside any { } then it cannot be used outside of it. You have to do ...

airplanesFile.writeUTF("Test");

Inside the try catch, in your case.

晨曦÷微暖 2024-11-11 13:20:22

因为一旦 try 块完成,您的airplanesFile 就超出了范围。请参阅局部变量声明的范围

Because your airplanesFile is out of scope once the try block is done. See Scope of Local Variable Declarations

浮云落日 2024-11-11 13:20:22

这是因为飞机文件是一个局部变量,仅存在于 try 块中。尝试阅读关于 java 中的变量作用域

That's because airplanesFile is a local variable and exists only in the try block. Try reading about variable scopes in java.

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