通俗地解释一下Java中的close()方法

发布于 2024-11-30 15:24:11 字数 885 浏览 2 评论 0原文

我学习了一个 java 教程,该教程允许我创建一个文本文件并在其中写入“20 Bruce Wayne”一词。主类中调用的最后一个方法名为 closeFile(),用于在创建文本文件后“关闭”文本文件。

如果我没有真正打开该文件,为什么需要“关闭”它?我所说的“打开”是指记事本编辑器(不是我正在使用的 IDE)会弹出“20 Bruce Wayne”字样。请用通俗的语言回答我的问题。

Main.java:createfile.java

class apple {
    public static void main(String[] args)
    {
        createfile g = new createfile();
        g.openFile();
        g.addRecords();
        g.closeFile();
    }
}

public class createfile {
    private Formatter x;

    public void openFile(){
        try{
            x = new Formatter("supermanvsbatman.txt");
        }
        catch(Exception e){
            System.out.println("you have an error");
        }
    }
    public void addRecords(){
        x.format("%s%s%s","20 ", "Bruce ", "Wayne ");
    }
    public void closeFile(){
        x.close();
    }
}

I went through a java tutorial that allowed me to create a text file and write the words,"20 Bruce Wayne" in it. The last method that is called in the main class is named closeFile() that "closes" the text file after it is created.

Why does the file need to be "closed" if I didn't really open it? By "open", I mean the Notepad editor(not the IDE I'm using) pops up with the words "20 Bruce Wayne". Please answer my question in layman's terms.

Main.java:

class apple {
    public static void main(String[] args)
    {
        createfile g = new createfile();
        g.openFile();
        g.addRecords();
        g.closeFile();
    }
}

createfile.java

public class createfile {
    private Formatter x;

    public void openFile(){
        try{
            x = new Formatter("supermanvsbatman.txt");
        }
        catch(Exception e){
            System.out.println("you have an error");
        }
    }
    public void addRecords(){
        x.format("%s%s%s","20 ", "Bruce ", "Wayne ");
    }
    public void closeFile(){
        x.close();
    }
}

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

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

发布评论

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

评论(4

执妄 2024-12-07 15:24:11

当文件被“打开”时,操作系统会将文件标记为锁定,通常这样它在使用时就不能被其他进程删除。 x.close() 取消锁定,允许操作系统和其他进程对文件执行其希望的操作。

When a file is "opened," the OS marks the file as locked, generally so it can't be deleted by other processes while it's being used. x.close() undoes the lock, allowing the OS and other processes to do what it wishes with the file.

痴意少年 2024-12-07 15:24:11

除了 Sold Out Activist 的答案之外,当您处理文件等 I/O 操作时,您正在使用流向文件添加文本,或从文件中提取文本。当您退出程序时,必须使用 close() 方法关闭该流,因为您可能会丢失数据。这就像保存操作,如果您不保存文件(关闭流),您将丢失对文件所做的更改。

请参阅此示例

In addition to the answer of Sold Out Activist, when you are working with i/o operations, such as files, you are using a stream to add text to your file, or extract text from your file. This stream must be closed, with the method close(), when you are exiting your program, because you could lose data. It's like a saving operation, if you don't save your file (close the stream), you will lose the changes made on file.

See this example, and this.

春庭雪 2024-12-07 15:24:11

用于关闭以写入模式打开的文件,因为为了减少/确保我们的数据安全,我们使用 close() 方法
它抛出像 (java.io.IOEXCEPTION) 这样的异常,为什么意味着任何与对象有关的方法调用,只是因为它是 public void close() 这意味着它是实例,所以它是与对象有关的调用,所以在某些时候是否有一个有机会让对象获得 null 任何与 null 引用相关的方法调用,然后它会得到 NullPointerException 所以这是代码
finally 块中的代码意味着我们打开的所有文件以及在finally 块中放弃的所有文件

is used for closing the file which is opened in write mode because to reduce/to make secure our data we use close() method
and it throws exception like (java.io.IOEXCEPTION) why means any method call with respect to object only because it is public void close() that means it is instance so it is calls with respect with object so in some times is there a chance to getting object to get null any method calls with respect to null reference then it getting NullPointerException so this is the code
code in finally block means what ever files we open that and all relinquished in finally block

无声情话 2024-12-07 15:24:11

Java Reader 类的 close() 方法用于关闭流并释放流中繁忙的资源(如果有)。此方法具有以下结果: 如果流打开,则关闭流以释放资源。如果流已经关闭,则不会有任何效果。

The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources. If the stream is already closed, it will have no effect.

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