Learn to append content to file in java using BufferedWritter, PrintWriter, FileOutputStream and Files class. In all the examples, while opening the file to write, you have pass a second argument as true which denotes that file is opened in append mode. Table of Contents Append to File using BufferedWritter Append to File using PrintWriter […]
When working on enterprise application, sometimes it is needed to write files in java e.g. writing reports in filesystem.. Though, there are multiple ways to do so, let’s quickly go through few of them for quick reference when it is needed.
Learn to read typesafe user inputs from console in any interactive java application i.e. prompt the user until user inputs the value in correct format/datatype.
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 […]
Many times we need to traverse and find all files with a certain extension only any do some operations on those files such as deleting them. This is more often required when you want to delete all log files from your log folder after certain time using your application program (if such requirement exist). In […]
If you want to write some content into a file in java using BufferedWriter, use below code as template and reuse it the way you like. 1) Using BufferedWriter without try-with-resources (Before Java 7) 2) Using BufferedWriter with try-with-resources (Java 7 Onwards) Happy Learning !!
If you want to read a file in java using BufferedReader, use below code as template and reuse it the way you like. BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. 1) Use BufferedReader without try-with-resources (Before Java 7) 2) Use […]
Creating a new file in java is very easy task and most of us are aware of this. Let’s list down 3 different ways to create new file for our beginner friends. Please add more ways in comment section if you are aware of and I will include it in post. 1) Using java.io.File class […]
This blog post mostly talks about how things related to I/O work at the lower level. This post is intended for readers who are curious to know that how java I/O operations are mapped at machine level; and what all things hardware does all the time when your application is running. I am assuming that […]
If your application needs to create temporary files for some application logic or unit testing, then you would like to make sure that these temporary files are deleted when they are not needed. Let’s learn how to delete temporary files in java. Deleting temporary file using File.deleteOnExit() or File.delete() To delete a file when apllication […]
Creating a temporary file in java can be required in many scenarios, but mostly it will happen during unit tests where you don’t want to store the results. As soon as testcase is finished, you do not care about file content. Creating temporary file using java.io.File.createTempFile() This example is simplest and most recommended solution. Creating […]