How to unzip a zip file in Java
This article shows how to use ZipInputStream and zip4j library to unzip a zip file in Java. To unzip a file manually, remember to add validation for the zip slip…
Favourite tutorials for developers
This article shows how to use ZipInputStream and zip4j library to unzip a zip file in Java. To unzip a file manually, remember to add validation for the zip slip…
This article shows a few examples to zip a single file and a whole directory (including sub-files and subdirectories): (1) Zip a file – java.util.zip, (2) Zip a file –…
This article shows how to convert a file into a hexadecimal (hex) representative format. The idea is to read the file into an InputStream and uses String.format(%X) to convert each…
This article shows a few ways to save a byte into a file. For JDK 1.7 and above, the NIO Files.write is the simplest solution to save byte to a…
In Java, we can use Files.readAllBytes(path) to convert a File object into a byte. Before Java 7, we can initiate a new byte with a predefined size (same with the…
In Java, we can use Files.exists(path) to test whether a file exists. The path can be a file or a directory. It is better to combine with !Files.isDirectory(path) to ensure…
This article shows few Java examples to get the total number of lines in a file. The steps are similar: (1) Open the file, (2) Read line by line, and…
In Java, we can use Files.size(path) to get the size of a file in bytes. This example uses NIO Files.size(path) to print the size of an image file (140 kb).…
In Java (@since 1.7), we can use the NIO Files.readAttributes to get all the file metadata, including the file creation date. We also can use the same code to get…
In Java, we can use the NIO Files.move(source, target) to rename or move a file. The Apache FileUtils.moveFile uses a "copy and delete" mechanism to rename or move a file.…