Skip to main content

Working with I/O Readers and Writers in Java

Introduction​

I/O readers and writers in Java provide higher-level abstractions for reading characters from and writing characters to streams. These classes are suitable for handling character-based data, such as text files. This guide covers basic operations with I/O readers and writers, including reading from and writing to files.

1. Reading from Readers​

Reading from a FileReader​

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

public class ReadFromReaderExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

2. Writing to Writers​

Writing to a FileWriter​

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

public class WriteToWriterExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, World!");
writer.newLine();
writer.write("This is a new line.");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

3. Closing Readers and Writers​

Closing a Reader or Writer​

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

public class CloseReaderExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
// Reader operations
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

4. Reader and Writer Properties​

Reader and Writer Properties​

Readers and writers provide various properties and methods for querying information about the stream, such as the current position, whether the stream is ready, and whether the end of the stream has been reached.

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

public class ReaderPropertiesExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
System.out.println("Is ready? " + reader.ready());
System.out.println("Has reached end of stream? " + (reader.readLine() == null));
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

Conclusion​

I/O readers and writers in Java provide convenient abstractions for working with character-based data. By using readers and writers, you can efficiently read from and write to files, streams, and other sources of character data. Understanding how to work with I/O readers and writers is essential for developing Java applications that handle text-based data effectively.

You can add this content to your Markdown file in Visual Studio Code by following the same steps as before.