Learn to read file content into string in java. Examples use Files.readAllBytes, Files.lines and FileReader & BufferedReader to read file content.
In this java example, we will learn to convert OutputStream to InputStream which you may need when you read data from one source which return an outputstream; and write/pass the data to other target which wants data in inputstream.
Example of java code to unzip and extract files and sub-directories from a compressed zip file using java.util.zip package.
This tutorial cover few examples of reading a file from resources folder in any java project. Table of Contents Project Structure Read file using ClassLoader.getResource().toURI() Read file using Spring’s ResourceUtils.getFile() Project Structure Below image describe the folder structure used in this example. Read file using ClassLoader.getResource().toURI() In this example, we will use the ClassLoader reference […]
A file in java can be read by N number of ways. Find below some good ways to read a file in java. Table of Contents Read file with BufferedReader Read file with try-with-resources Read file with java.nio.file.Files Read file with Apache Commons IOUtils Read file with Guava Read file with BufferedReader A most simple, […]
This tutorial covers creating password protected zip files using a very useful library zip4j. Java, by default, does not provide any support for password protection for file; though it has very good API support for creating/extracting zip files. There are some other useful libraries out there, which are equally good and sometimes better than zip4j, […]
To copy a directory from one location to another with all it’s sub-folders and files they contain, use below code which uses recursion to traverse the directory structure and then uses Files.copy() function to copy files. Folder copy example sourcecode In this example, I am copying all the sub directories and files under c:\temp to […]
A checksum hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user provided content. In this post, we will learn to generate the checksum hash for files. Why we may want to generate checksum hash for a file? Any serious file providers provide a mechanism to have a checksum […]
To make a file read only in java, you can use one of below methods. 3rd method using Runtime.getRuntime().exec() is platform specific because of command you pass to it as parameter. Rest two methods will work fine in most cases. 1) Use java.io.File.setReadOnly() method 2) Use java.io.File.setWritable(false) method 3) Execute a native command (platform dependent) […]
Copying a file from one place to another in java is a common task which you need to do in your application. In this example, I am listing out 4 different ways to copy a file in java. Use one which best fit your need. 1) Copy file using apache commons IO To use this […]