8

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();
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user701510
  • 5,563
  • 17
  • 61
  • 86

4 Answers4

13

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.

  • 2
    @user701510: a number of things can go wrong depending on the storage you use in the background as well as the operating system, e.g. never closing a file may result in reaching the max number of open file handles in your OS. Certain storage system may only flush the file to disk if you close it... – home Aug 19 '11 at 08:44
  • In certain operating systems, if you don't close the file, the pointer in memory, if not the memory holding entire file, which you won't get back until you reboot. –  Aug 19 '11 at 08:54
6

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.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • 3
    what does it mean to flush a stream? – user701510 Aug 19 '11 at 09:16
  • 4
    Flush() is a method that allows you to unload the stream. You will flush a stream for efficiency reasons, because, for example, if you are writing a line in a file, this line is buffered, putted on a buffer, and flushing the stream, this line is immediately written to file and the buffer you are using will be cleaned, ready to receive other data. Normally, when you call close(), both you are calling flush() to clean the buffer, write data, and closing the stream. See this for further info about the methods: http://download.oracle.com/javase/6/docs/api/java/io/OutputStream.html – Alberto Solano Aug 19 '11 at 09:30
1

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

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.

Sumit Mukharjee
  • 312
  • 2
  • 9