Should the OutputStream object (created as an expression in a method call, but not assigned to a variable) still be closed?

I am trying to use FileOutputStream anonymously to store a property using the java.util.Property storage method.

Properties p = new Properties();
. . .
p.store(new FileOutputStream("nameOfFile"), "Created by me.");

Reading javadocs for the store method says that the stream will be cleared and will remain open after the method returns.

I am wondering what happens to the flow. Since I installed it anonymously, I assume that the object will be garbage collected immediately (if this is incorrect, please correct me). However, I was taught to always close my flows when I finished with them. In this case, I cannot close the stream, since I do not have an object referencing it. Is there any need to worry about the thread staying open here, or will Java take care of this in this case by immediately collecting the object?

+4
source share
3 answers

This is not an anonymous object (or rather an anonymous class). It is simply not an assigned object.

store , GC'd. store , , ​​ store. close, , , finalize close ad other do not, - GC.

, , close.

try-with-resources ( Java > try-with-resources) :

Properties p = new Properties();

try (FileOutputStream stm = new FileOutputStream("nameOfFile"))
{
    p.store(stm, "Created by me.");     
} catch (IOException e) {
    // TODO: handle this
}

, ( ) Autocloseable, close , close close. , close , close e.getSuppressed().

Autocloseable.close() , , - . Closeable, . :

, Closeable

Java 6 , , , , close .

+4

, , finalize .

finalize FileOutputStream, , close:

/**
 * Cleans up the connection to the file, and ensures that the
 * <code>close</code> method of this file output stream is
 * called when there are no more references to this stream.
 *
 * @exception  IOException  if an I/O error occurs.
 * @see        java.io.FileInputStream#close()
 */
protected void finalize() throws IOException {
    if (fd != null) {
        if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
            flush();
        } else {

            /*
             * Finalizer should not release the FileDescriptor if another
             * stream is still using it. If the user directly invokes
             * close() then the FileDescriptor is also released.
             */
            runningFinalize.set(Boolean.TRUE);
            try {
                close();
            } finally {
                runningFinalize.set(Boolean.FALSE);
            }
        }
    }
}

try-with-resources, , , ,

try(FileOutputStream outStream = new FileOutputStream("nameOfFile")) {
     Properties p = new Properties();
     . . .
     p.store(outStream, "Created by me.");
}
+2

...

FileOutputStream fos = new FileOutputStream("nameOfFile")

...

p.store(fos, "Created by me.");

.

The Properties.store () command does not support a stream reference, therefore there is no reference to it, and therefore, it will be GC'ed after the method returns, since this is the volume of the stream area.

0
source

All Articles