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.
Learn to use System.getProperty(“user.dir”) and Paths.get(“”).toAbsolutePath() to get current working directory or root folder of the application.
Two Java examples of reading a file from resources folder in either simple Java application or any spring mvc / spring boot application.
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 […]
Java create new file examples. Creating a new file in Java is a very easy task and most of us are aware of this. Let’s look at 3 most commonly used ways.
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 […]