Introduction

  • Java provides several APIs (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.

I/O Streams

  • There are two types of I/O Streams used to interact with files in Java:
    1. Character Streams
    2. Byte Streams

1. Character Streams

    • In Java, a character stream is a sequence of characters that can be read or written by using classes provided by the Java I/O (Input/Output) library.
    • Character streams are designed to handle Unicode characters, allowing us to work with text data efficiently.
    • Character Streams are used to read or write the character’s data type.
    • All the Character Streams classes are defined under java.io package.
    • The Java I/O library provides two main abstract classes for character stream operations: Reader and Writer.
      1. Reader Class: This abstract class serves as the base class for reading character streams. Some sub-classes of Character Streams that can be used to Read Character (Reader) data from a file or console are:-
        • StringReader: This sub-class reads characters from a string.
        • InputStreamReader: This sub-lass is used to read the byte stream and converts it to the character stream. This reads characters from an input stream (e.g., System.in).
        • FileReader: This is a sub-class to read the characters from a file.
        • BufferedReader:
          1. This is a wrapper over the Reader class that supports buffering capabilities.
          2. In many cases, this is the most preferable class to read data because more data can be read from the file in one read() call, reducing the number of actual I/O operations with the file system.
      2. Writer Class: This abstract class serves as the base class for writing character streams. Some sub-classes of Character Streams that can be used to Write Character (Writer) data to a file are:-
        • StringWriter:  This sub-class writes characters to a string.
        • OutputStreamWriter: This sub-class is used to write character streams and also convert them to byte streams.
        • FileWriter: This is a sub-class to actually write characters to the file.
        • BufferedWriter:
          1. This is a wrapper over the Writer class, which also supports buffering capabilities.
          2. This is the most preferable class to write data to a file since more data can be written to the file in one write() call.
          3. Like BufferedReader , this reduces the number of total I/O operations with the file system.

2. Byte Streams

    • In Java, a byte stream is a sequence of bytes that can be read from or written to using classes provided by the Java I/O (Input/Output) library.
    • Byte streams are designed to handle binary data, such as files, network sockets, or any other data that is not text-based.
    • Byte Streams are used to read or write byte data with files.
    • This is different from Character Streams in the way they treat the data.
    • Here we work with raw bytes, which could be characters, image data, Unicode data (which takes 2 bytes to represent a character), etc.
    • All of the classes of Byte Streams are defined under java.io package.
    • The Java I/O library provides two main abstract classes for byte stream operations: InputStream and OutputStream.
      1. InputStream: This is an abstract class to read the byte streams. This abstract class serves as the base class for reading byte streams. Some sub-classes of Byte Streams that can be used to Read the Byte data from a file are:-
        • FileInputStream: This is a sub-class to simply read bytes from a file.
        • ByteStreamInputStream: Reads bytes from an in-memory byte array.
        • SocketInputStream: Reads bytes from a socket connection.
        • BufferedInputStream:
          1. This is a wrapper  InputStream that supports buffering capabilities.
          2. As we saw in the character streams, this is a more efficient method than FileInputStream.
      1. OutputStream: This abstract class serves as the base class for writing byte streams. Some sub-classes of Byte Streams that can be used to Write the Byte data to a file or console are:-
        • ByteArrayOutputStream: Writes bytes to an in-memory byte array.
        • SocketOutputStream: Writes bytes to a socket connection.
        • FileOutputStream: This is a sub-class to write raw bytes to the file.
        • ByteOutputStream:
          1. This class is a wrapper  OutputStream to support buffering capabilities.
          2. And again, as we saw in the character streams, this is a more efficient method than FileOutputStream thanks to the buffering.

Loading

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.