Introduction

When programming, whether you’re creating a mobile app, a web application, or just writing scripts, you often have the need to read or write data to a file in your application. This data could be cache data, data you retrieved for a dataset, an image, or just about anything else you can think of.

In this tutorial, we are going to show the most common ways you can read and write to files in Java.

Java provides several API (also known as Java I/O) to read and write files since its initial releases. With subsequent releases, Java I/O has been improved, simplified and enhanced to support new features.

Before we get in to some actual examples, it would help to understand the classes available to you that will handle the reading and writing of data to files. In the following sections we’ll provide a brief overview of the Java I/O classes and explain what they do, then we’ll take a look at Java NIO Streams, and finally we’ll show some examples of reading and writing data to files.

I/O Streams

There are two types of Streams you can use to interact with files:

  1. Character Streams
  2. Byte Streams

For each of the above stream types, there are several supporting classes shipped with Java, which we’ll take a quick look at below.

Character Streams

Character Streams are used to read or write the characters data type. Let’s look at the most commonly used classes. All of these classes are defined under java.io package.

Here are some classes you should know that can be used to read character data:

  • Reader: An abstract class to read a character stream.
  • InputStreamReader: Class used to read the byte stream and converts to character stream.
  • FileReader: A class to read the characters from a file.
  • BufferedReader: This is a wrapper over the Reader class that supports buffering capabilities. In many cases this is most preferable class to read data because more data can been read from the file in one read() call, reducing the number of actual I/O operations with file system.

And here are some classes you can use to write character data to a file:

  • Writer: This is an abstract class to write the character streams.
  • OutputStreamWriter: This class is used to write character streams and also convert them to byte streams.
  • FileWriter: A class to actually write characters to the file.
  • BufferedWriter: This is a wrapper over the Writer class, which also supports buffering capabilities. This is most preferable class to write data to a file since more data can be written to the file in one write() call. And like the BufferedReader, this reduces the number of total I/O operations with file system.

Byte Streams

Byte Streams are used to read or write byte data with files. This is different from before in the way they treat the data. Here you work with raw bytes, which could be characters, image data, unicode data (which takes 2 bytes to represent a character), etc.

In this section we’ll take a look at the most commonly used classes. All of these classes are defined under java.io package.

Here are the classes used to read the byte data:

  • InputStream: An abstract class to read the byte streams.
  • FileInputStream: A class to simply read bytes from a file.
  • BufferedInputStream: This is a wrapper over InputStream that supports buffering capabilities. As we saw in the character streams, this is a more efficient method than FileInputStream.

And here are the classes used to write the byte data:

  • OutputStream: An abstract class to write byte streams.
  • FileOutputStream: A class to write raw bytes to the file.
  • ByteOutputStream: This class is a wrapper over OutputStream to support buffering capabilities. And again, as we saw in the character streams, this is a more efficient method than FileOutputStream thanks to the buffering.

 349 total views,  2 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.