Java IO Tutorial

Java IO Tutorial
This is a complete up-to-date tutorial for File Handling in Java. The source code of this guide is well tested in our local development environment and code is analyzed with static code analysis tools SonatLint, PMD etc.

The source code from this guide is bug-free and used JDK 8 to compile and execute the source code.

Java I/O Basics

In this article, we will learn the basics of Java I/O, an overview of different I/O Streams from the java.io package, overview of the classes in the Java IO (java.io) package and Java IO Purposes and Features.
In this post, we will learn how to develop programs which use byte streams to perform input and output of 8-bit bytes.

Java I/O Character Streams
In this post, we will discuss character stream I/O classes with examples.

In this article, we will discuss four buffered stream classes used to wrap unbuffered streams with examples.
We will look into the object stream classes are ObjectInputStream and ObjectOutputStream. These classes implement ObjectInput and ObjectOutput, which are subinterfaces of DataInput and DataOutput.
In this article, we will discuss Data Stream classes DataInputStream and DataOutputStream with examples.

Java I/O Classes

In this article, we will discuss FileOutputStream Class constructors, Methods/APIs with examples.

FileInputStream Class in Java
In this article, we will discuss FileInputStream Class constructors, Methods/APIs with examples.

In this article, we will discuss ByteArrayOutputStream Class constructors, Methods/APIs with examples.
In this article, we will discuss ByteArrayInputStream Class constructors, Methods/APIs with examples.
In this article, we will discuss BufferedWriter Class constructors, Methods/APIs with examples.
In this article, we will discuss BufferedReader Class constructors, Methods/APIs with examples.
In this article, we will discuss BufferedOutputStream Class constructors, Methods/APIs with examples.
In this article, we will discuss BufferedInputStream Class constructors, Methods/APIs with examples.
In this article, we will discuss FileWriter Class constructors, Methods/APIs with examples.
In this article, we will discuss FileReader Class constructors, Methods/APIs with examples.
In this article, we will discuss DataOutStream Class constructors, Methods/APIs with examples.
In this article, we will discuss DataInputStream Class constructors, Methods/APIs with examples.
In this article, we will discuss ObjectOutputStream Class constructors, Methods/APIs with examples.
In this article, we will discuss ObjectInputStream Class constructors, Methods/APIs with examples.

File Handling Examples

How to create a new file in java
In this article, we will learn how to create a file with Java program.
In this example, we will use BufferedWriter class to write the file. There are another Stream Output classes to write file but in this example, we will focus on frequently used BufferedWriter.
It is very simple to delete a file from a specific directory. Java File class provides delete() method can be used to delete files or empty directory/folder in java.
This example shows how to copy a file using Java program.
This example shows how to move a file using Java program.
In this example, we will renameTo() method to rename a file.
This example shows how to append a file using a Java program.

In this example, we will use a BufferedReader class to read a file named "sample.txt". BufferedReader class is used to read the text from a character-based input stream. It can be used to read data line by line by readLine() method. It makes the performance fast.
In this example, we will use a BufferedInputStream class to read a file. The BufferedInputStream class is used to read information from the stream. It internally uses a buffer mechanism to make the performance fast.
In this example, we will DataInputStream class to read a file. A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.
In this example, we will write a generic method to get file size in bytes, kilobytes, megabytes, GB, TB.

File Serialization Examples

How to write an Object to file in Java
In this example, we will use ObjectOutputStream class to write an employee object to file.
In this example, we will use ObjectInputStream class to read employee object to file.

File Compression Examples

Compress Files in ZIP format in Java
In this article, we will show you how to compress a single file to zipping and multiple files to zip format. The example from this article uses the try-with-resources Statement to auto close the resources and compiled and executed on JDK 8 and later.
In this article, we will show you how to unzip it. The example from this article uses the try-with-resources Statement to auto close the resources and compiled and executed on JDK 8 and later.

Directory Handling Examples

The current working directory means the root folder of your current Java project, it can be retrieved by using the following system property function.
In this example, we will create a Java program to create a directory on a file system.

Delete directory
In this example, we will delete a directory using Java program.

We will write a Java program to copy directory.

Traverse /List a directory structure
We will list out a number of files under directory using Java program.

Java program to check if a directory is empty or not.

File Utilities (Development)

In this article, we will show you the list commonly used file utility methods. This File Utility class contains more than 20 file utility methods and all are commonly used methods. Hope this FileUtils.java class will help.

Java Zip Utility Class
In this article, we will discuss a few commonly used Java Zip utility methods. We can also show you the sample JUnit test cases for zip utility methods. ZipUtils contains below generic methods.

In this article, we will discuss 20 useful commonly used file utility methods. You can use these file utility methods in your day to day project work. I found these utility methods are useful and I thought I should share with you all.

Java I/O Reference

Comments