Java Input Output Tutorial with Examples


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

  • Overview of Java I/O - 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.
  • Java I/O Byte Streams - 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 Buffered Streams - In this article, we will discuss four buffered stream classes used to wrap unbuffered streams with examples.
  • Java I/O Object Streams - 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.
  • Java I/O Data Streams - In this article, we will discuss Data Stream classes DataInputStream and DataOutputStream with examples.

Java I/O Classes

  • FileWriter Class in Java - In this article, we will discuss FileWriter Class constructors, Methods/APIs with examples.
  • FileReader Class in Java - In this article, we will discuss FileReader Class constructors, Methods/APIs with examples.

File Handling Examples

  • How to Write File in Java - In this example, we will use a BufferedWriter class to write the file. There are other Stream Output classes to write the file but in this example, we will focus on frequently used BufferedWriter.
  • How to delete a file in Java - 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.
  • Read File using BufferReader - 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 the data line by line by the readLine() method. It makes the performance fast.
  • Read File using BufferInputStream - 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.
  • Read File using DataInputStream - 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.

File Serialization Examples

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.
  • Decompress Files from a ZIP file in Java - 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

  • Create a directory - 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 a Java program.

File Utilities (Development)

  • Java File Utility Class - 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.
  • Top File Utility 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

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course