Overview of Basic Input and Output in Java


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Basic Input and Output in Java </aside>

<aside> 💡

  1. Types of I/O Streams </aside>

<aside> 💡

  1. Differences Between Byte Streams and Character Streams </aside>

Basic Input and Output in Java


  1. Java I/O

    1. I/O in Java (Input and Output) deals with reading data from sources (input) and writing data to destinations (output). Java provides a rich set of classes under the java.io package for performing input and output operations. These operations include file handling, console input/output, object serialization, and more.

  2. Key Concepts of Java I/O

    1. Stream:
      1. A stream is a sequence of data.
      2. Input Stream: Reads data from a source.
      3. Output Stream: Writes data to a destination.
      4. Examples: Reading/writing files, console input/output.
    2. Byte Streams vs. Character Streams:
      1. Byte Streams:
        1. Handles raw binary data (e.g., images, videos).
        2. Classes: InputStream, OutputStream.
      2. Character Streams:
        1. Handles textual data (e.g., reading/writing text files).
        2. Classes: Reader, Writer.
    3. Serialization:
      1. A mechanism of converting an object into a byte stream for storage or transmission.
      2. Classes: ObjectInputStream, ObjectOutputStream.

  3. Types of Streams

    1. Byte Streams
      1. Designed to handle binary data.
      2. Classes:
        1. InputStream (Abstract class): Reads byte data.
          1. Examples: FileInputStream, BufferedInputStream.
        2. OutputStream (Abstract class): Writes byte data.
          1. Examples: FileOutputStream, BufferedOutputStream.
    2. Character Streams
      1. Designed to handle text data (characters).
      2. Classes:
        1. Reader (Abstract class): Reads character data.
          1. Examples: FileReader, BufferedReader.
        2. Writer (Abstract class): Writes character data.
          1. Examples: FileWriter, BufferedWriter.

  4. Best Practices for Java I/O

    1. When working with Java I/O, consider the following best practices:
    2. Use Try-with-Resources: Always use the try-with-resources statement when working with streams to ensure proper resource management. This automatically closes the streams when they are no longer needed, preventing resource leaks.
    3. Use Buffered I/O: Whenever possible, use buffered I/O classes to minimize the number of system calls and improve performance.
    4. Handle Exceptions Gracefully: Handle I/O exceptions gracefully by implementing error handling mechanisms such as logging or error propagation.
    5. Use NIO for Performance: For high-performance I/O operations, consider using Java's NIO (New I/O) package, which provides non-blocking I/O features and enhanced performance.

  5. File Handling in Java

    1. File handling allows reading from and writing to files. Common classes used are:

    2. File Class

      1. Represents file and directory pathnames.
    3. Methods:

      1. createNewFile(): Creates a new file.
      2. mkdir(): Creates a new directory.
      3. exists(): Checks if the file exists.
      4. delete(): Deletes the file.
    4. Example:

      import java.io.File;
      
      public class FileExample {
          public static void main(String[] args) {
              File file = new File("example.txt");
      
              try {
                  if (file.createNewFile()) {
                      System.out.println("File created: " + file.getName());
                  } else {
                      System.out.println("File already exists.");
                  }
              } catch (Exception e) {
                  System.out.println("An error occurred.");
                  e.printStackTrace();
              }
          }
      }