December 7, 2023

How to delete directory in Java

If we use the NIO Files.delete to delete a non-empty directory in Java, it throws DirectoryNotEmptyException; for legacy IO File.delete to delete a non-empty directory, it returns a false. The standard solution is to loop the directory recursively, and delete all its children’s contents first (sub-files or sub-directories), and delete the parent later. This example shows some common ways to delete a directory in Java: (1) Files.walkFileTree + FileVisitor (Java 7), (2) Files.walk (Java 8), (3) FileUtils.deleteDirectory (Apache Common IO), (4) Recursive delete in a directory (Plain Java code).

How to delete directory in Java Read More

How to create directory in Java

In Java, we can use the NIO Files.createDirectory to create a directory or Files.createDirectories to create a directory including all nonexistent parent directories. For legacy IO java.io.File, the similar methods are file.mkdir() to create a directory, and file.mkdirs() to create a directory including all nonexistent parent directories. In legacy IO, the lack of exception thrown in creating directory makes developers very hard to debug or understand why we cannot create a directory, and this is one of the reasons Java releases a new java.nio.Files to throw a proper exception.

How to create directory in Java Read More

How to write data to a temporary file in Java

The temporary file is just a regular file created on a predefined directory. In Java, we can use the NIO Files.write() to write data to a temporary file. In Java, there are many ways to write data or text to a temporary file; it works like writing to a regular file. You can choose BufferedWriter, FileWriter etc, but in most cases, the NIO java.nio.Files should be enough to write or append data to a file.

How to write data to a temporary file in Java Read More

How to create a temporary file in Java

In Java, we can use the Java NIO Files.createTempFile() methods to create a temporary file in the default temporary-file directory. The default temporary file folder is vary on operating system: Windows – %USER%\AppData\Local\Temp, Linux – /tmp . We can use the legacy Java IO java.io.* to create a temporary file, it still works perfectly, but the new Java NIO should always be the first choice.

How to create a temporary file in Java Read More

Java – Compress a file in Gzip format

This article shows how to use Java to compress a file in Gzip format. GZip is a standard file compression tool on Unix or Linux system, and generally has the suffix .gz. It makes smaller file size, save disk space. Gzip compress a single file, and the Tar is collecting files into one archive file. We copy the FileInputStream into a GZIPOutputStream to compress a file in Gzip format. Instead, we use NIO Files.copy to copy a Path to GZIPOutputStream directly.

Java – Compress a file in Gzip format Read More

How to create tar.gz in Java

The tar is for collecting files into one archive file, aka tarball, and generally has the suffix .tar. The Gzip is for compress files to save space and generally has the suffix .gz. The tar.gz or .tgz means group all files into one archive file, and compress it using Gzip. In Java, we have ZipOutputStream to create a zip file, and GZIPOutputStream to compress a file using Gzip, but there is no official API to create a tar.gz file. In Java, we can use Apache Commons Compress (Still active in development) to create a .tar.gz file.

How to create tar.gz in Java Read More