Trying to create a ZIP file having 3 files inside it. I am using ZipOutputStream for creating ZIP file and Printwriter to create and write files into Zip.
When i close printwriter object after writing a file closes the stream as well, which i know. But if i close zip output stream after writing into zip will it close and clean my printwriter objects as well
def main(args: Array[String]): Unit = {
val file: File = new File("Hello.zip")
val zos: ZipOutputStream = new ZipOutputStream(new FileOutputStream(file))
val fileNames = Array("Happy1.csv", "Happy2.csv", "Happy3.csv", "Happy4.csv")
fileNames.foreach { tempfile =>
zos.putNextEntry(new ZipEntry(tempfile))
Try {
val writer = new PrintWriter(new OutputStreamWriter(zos))
writer.println("Hello,World")
writer.flush()
// writer.close() // Closing writer closes the stream so commented it
}
}
zos.closeEntry()
zos.flush()
zos.close() // will my writer object also closed and cleaned by gc
}