explain the close() method in Java in Layman's terms

前端 未结 3 668
南笙
南笙 2020-12-18 06:41

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

相关标签:
3条回答
  • 2020-12-18 07:04

    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.

    0 讨论(0)
  • 2020-12-18 07:10

    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

    0 讨论(0)
  • 2020-12-18 07:25

    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.

    0 讨论(0)
提交回复
热议问题