Skip to Content
Welcome to my more educational website. Head back to more fun stuff here!

File IO and Exceptions in Java

File I/O allows a Java program to read data from files and write data to files. Java treats files as streams of data and provides classes to handle input and output operations.

File Input vs File Output

  • Input: reading data from a file into a program
  • Output: writing data from a program to a file

Java distinguishes these clearly through different classes.


FileInputStream (Reading Files)

FileInputStream is used to read raw bytes from a file.

FileInputStream fis = new FileInputStream("data.txt");

If the file does not exist, Java throws a FileNotFoundException. This exception is thrown when the stream is created and not when you read.

new FileInputStream("missing.txt"); // FileNotFoundException

FileOutputStream (Writing Files)

FileOutputStream is used to write bytes to a file.

FileOutputStream fos = new FileOutputStream("out.txt");

Tf the file does not exist, Java creates it and If the file exists, it is overwritten by default.

Closing Streams

Exception Handling in Java

An exception is an event that occurs during program execution that disrupts the normal flow of instructions. In Java, exception handling is designed to separate error-handling logic from regular program logic, making programs adept at handling errors instead of crashing immediately when an error occurs.

At the core of Java’s exception-handling mechanism are the try, catch, and finally blocks. Code that may cause an exception is placed inside a try block. If an exception occurs, Java immediately stops executing the remaining statements in that try block and looks for a matching catch block to handle the exception. The optional finally block contains code that executes regardless of whether an exception was thrown or caught.

try { // code that may throw an exception } catch (ExceptionType e) { // code that handles the exception } finally { // code that always runs }

When an Exception Is thrown, execution immediately stops at the throw point. If an exception is caught, the program continues after the try-catch-finally structure. If an exception is not caught, the program halts and remaining code in the try block is skipped.

Checked vs Unchecked Exceptions

Java divides exceptions into checked and unchecked categories. This distinction determines whether the compiler forces the programmer to handle the exception.

Checked Exceptions

Checked exceptions represent conditions that a program is expected to handle. The Java compiler requires these exceptions to be either caught using a try-catch block or declared in the method signature using the throws keyword.

Common checked exceptions include IOException and FileNotFoundException, which typically arise during file operations.

public void readFile() throws FileNotFoundException { // file access code }

If a checked exception is neither caught nor declared, the program will not compile. This forces developers to explicitly account for recoverable error conditions and are needed for File operations.

Unchecked Exceptions (Runtime Exceptions)

Unchecked exceptions are subclasses of RuntimeException and usually indicate programming errors rather than recoverable conditions. These exceptions are not required to be caught or declared.

Common unchecked exceptions include ArithmeticException, NullPointerException, and ClassCastException.

int x = 1 / 0; // ArithmeticException

Unchecked exceptions can be caught, but good program design focuses on preventing them through correct logic rather than handling errors.

Last updated on