java.io Package

java.io Package

Class/Interface Description
File A class for handling file and directory operations.
InputStream An abstract class representing an input stream of bytes.
OutputStream An abstract class representing an output stream of bytes.
Reader An abstract class for reading characters from a stream.
Writer An abstract class for writing characters to a stream.

Example: File Input/Output

In this example, we'll demonstrate reading from and writing to a file using the classes from the `java.io` package.

Class Example
File
          import java.io.File;
          import java.io.IOException;

          public class FileExample {
            public static void main(String[] args) {
              try {
                // Create a File object representing a file
                File file = new File("example.txt");

                // Check if the file exists
                if (file.exists()) {
                  System.out.println("File already exists.");
                } else {
                  // Create a new file
                  boolean created = file.createNewFile();
                  if (created) {
                    System.out.println("File created successfully.");
                  } else {
                    System.out.println("Failed to create file.");
                  }
                }
              } catch (IOException e) {
                System.out.println("An error occurred: " + e.getMessage());
              }
            }
          }
        
InputStream/OutputStream
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;

          public class FileIOExample {
            public static void main(String[] args) {
              try {
                // Create an InputStream to read from a file
                FileInputStream inputStream = new FileInputStream("input.txt");

                // Create an OutputStream to write to a file
                FileOutputStream outputStream = new FileOutputStream("output.txt");

                // Read from the input file and write to the output file
                int data;
                while ((data = inputStream.read()) != -1) {
                  outputStream.write(data);
                }

                // Close the streams
                inputStream.close();
                outputStream.close();

                System.out.println("File copied successfully.");
              } catch (IOException e) {
                System.out.println("An error occurred: " + e.getMessage());
              }
            }
          }
        

In this example, we demonstrate two scenarios using classes from the java.io package:

  1. Creating and checking a file using the File class:
      We create a File object representing a file named "example.txt".
      We check if the file already exists or create a new file if it doesn't.
      The example code handles potential exceptions.
  2. Reading from and writing to a file using InputStream and OutputStream classes:
      We create an InputStream from an input file and an OutputStream for an output file.
      We read data from the input file and write it to the output file.
      Finally, we close the streams and print a success message.

These examples showcase the basic usage of classes from the java.io package for file handling operations.

Go To Core Java Page Click Here

Post a Comment

Previous Post Next Post