Why is it necessary to close file?

Asked

Viewed 350 times

4

Why is this line required in Java?

fout.close();

Context:

LinkedList values = new LinkedList(classe.getTurma().values());

    if (!values.isEmpty()) {

        FileOutputStream fout = null;
        String FILE = "TURMA";
        try {
            fout = new FileOutputStream(FILE);
            System.out.println("Tamanhno tuma" + values.size());
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(values);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Sistema.class
                    .getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {
            Logger.getLogger(Sistema.class
                    .getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fout.close();

            } catch (IOException ex) {
                Logger.getLogger(Sistema.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
  • 1

    Where did you get this piece? Add an example, otherwise it is difficult to understand the problem.

  • I think what he is wondering is why it is necessary to close files as a whole, not in a specific application, if that is why an open file will take up unnecessary memory space, and why it is important to close files after use. I once had a problem where I forgot to close the requisition with a bank and because of that I suffered from memory overflow.

  • put the stretch down

  • @Javinha Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

12

It may be for several reasons:

  • One of them is to make the recording take place. It has several file manipulation modes that only play on disk if you really need to or when to close. So without closing is not recorded.
  • If you let the operating system close when the process that holds it closes it may not only not record everything it needs, but it may record what it shouldn’t.
  • It may be because the file needs to be used by another process and needs to be closed for this to be possible. Unless the file was shared.
  • The open file occupies operating system resources such as handlers, memory and needs some mechanism to handle it. Memory consumption in part is "charged" from your application, which can create complications if you use too much.

By the example of the question posted later, I think only the second item does not apply, it is more rare even. Just note that the 3 catches do the same thing and it is redundant to do so, it is possible to simplify.

4

The close() in any programming language and operating system is basically used to:

  1. If the operating system has limited resources, for example number of files opened on embedded system processors, you are wasting system resources if you do not close the files with the close().

  2. If the file has any kind of buffer behind it and you don’t make a close() you may lose data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.