Skip to main content Link Menu Expand (external link) Document Search Copy Copied

File Input and Output

Coding Pirate

Ahoy, matey! Ready to set sail on the high seas of Java programming? Today, we’ll embark on a thrilling adventure to explore the treasure trove of Java’s file input and output (I/O) capabilities. Just as a skilled pirate navigates treacherous waters, you’ll learn how to read from and write to files, an essential skill for any Java sailor.

X Marks the Spot: Java File Class

In the vast Java ocean, the File class is like a treasure map that leads us to hidden riches. This class allows you to interact with files and directories on your system. Imagine you’ve discovered a secret parchment containing the location of a buried treasure chest. The File class is your key to unlocking its mysteries.

import java.io.File;

File treasureMap = new File("treasure_map.txt");

Hoist the Jolly Roger: Reading Files

When you’ve got your hands on a treasure map, the first step is to decipher its contents. In Java, you can read files using the FileReader and BufferedReader classes. These two classes work together like a swashbuckling duo to retrieve information from your files.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

File treasureMap = new File("treasure_map.txt");

try (FileReader fr = new FileReader(treasureMap);
     BufferedReader br = new BufferedReader(fr)) {

    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    System.err.println("Blimey! Something went wrong: " + e.getMessage());
}

Plundering the Booty: Writing Files

Once you’ve deciphered the treasure map, it’s time to make your mark and claim the booty. In Java, you can write to files using the FileWriter and BufferedWriter classes. As you sail through the file, these classes allow you to record your findings like a trusty logbook.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

File treasureLog = new File("treasure_log.txt");

try (FileWriter fw = new FileWriter(treasureLog);
     BufferedWriter bw = new BufferedWriter(fw)) {

    bw.write("Found treasure at the Isle of Java!");
} catch (IOException e) {
    System.err.println("Shiver me timbers! We hit an error: " + e.getMessage());
}

As your Java pirate adventures continue, you might encounter stormy seas that call for more advanced navigation techniques. Java’s New I/O (NIO) package offers a modern, high-performance alternative to traditional I/O classes. With the Path and Files classes, you’ll be able to conquer even the most challenging file I/O tasks.

```java import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.io.IOException; import java.util.List;

Path treasureMapPath = Paths.get(“treasure_map.txt”);

try { List lines = Files.readAllLines(treasureMapPath); lines.forEach(System.out::println); } catch (IOException e) { System.err.println("Yarr! We be in a tight spot: " + e.getMessage()); }