Java's try-catch-resources flush automatically?

3

When using an object of type FileInputStream and FileOutputStream inside a try-catch-resources java automatically uses the close () but is flush () automatic or not?

// Exemplo
try(FileOutputStream fos = new FileOutputStream("arquivo.txt")){
    try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
        bos.write("Teste");
        bos.flush(); // Precisa usar o flush()
    }
    fos.flush(); // Precisa usar o flush()
}
    
asked by anonymous 17.10.2016 / 08:24

1 answer

0

The flush is automatic or not depends on the implementation of the concrete class you use.

The try with resources only expects Closeable or AutoCloseable objects, it is not directly connected to the flush, but some implementations of these interfaces execute the flush before the close.

link

    
19.10.2016 / 19:06