FileReader class in Java



Introduction:

File handling is a fundamental aspect of programming, and Java provides robust APIs to manipulate files. The `FileReader` class is a key player when it comes to reading data from files in Java. In this blog post, we will explore the FileReader class and its various functionalities through 10 different code examples.

Example 1: Basic FileReader Initialization


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

public class Example1 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("example.txt")) {
int character;
while ((character = fileReader.read()) != -1) {
System.out.print((char) character);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: This example demonstrates how to initialize a `FileReader` and read characters from a file using its `read()` method.

Example 2: Reading Characters into a Char Array


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

public class Example2 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("example.txt")) {
char[] buffer = new char[1024];
int bytesRead;
while ((bytesRead = fileReader.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, bytesRead));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: In this example, we read characters into a char array to efficiently handle larger chunks of data.


Example 3: Using BufferedReader with FileReader


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

public class Example3 {
public static void main(String[] args) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Here, we introduce `BufferedReader` to read lines from the file, enhancing performance.

Example 4: FileReader with Try-With-Resources


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

public class Example4 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("example.txt")) {
// File reading logic here
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates the use of try-with-resources to automatically close the FileReader.


Example 5: Skip Characters using skip()


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

public class Example5 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("example.txt")) {
fileReader.skip(5); // Skip the first 5 characters
// Continue with the reading logic
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Illustrates skipping a specified number of characters using the `skip()` method.

Example 6: FileReader with Custom Charset


import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Example6 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("example.txt", StandardCharsets.UTF_8)) {
// File reading logic here
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Shows how to specify a custom character set when creating a FileReader.


Example 7: FileReader for Binary Data


import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class Example7 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader(new FileInputStream("binaryFile.bin"))) {
// File reading logic for binary data
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates using `FileReader` with a `FileInputStream` for reading binary data.

Example 8: FileReader with Mark and Reset


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

public class Example8 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("example.txt")) {
if (fileReader.markSupported()) {
fileReader.mark(10); // Mark the current position
// Continue with the reading logic
fileReader.reset(); // Reset to the marked position
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Introduces the `mark()` and `reset()` methods for setting and returning to a marked position.


Example 9: Handling FileNotFound Exception


import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Example9 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader("nonexistentFile.txt")) {
// File reading logic here
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Demonstrates how to handle `FileNotFoundException` when attempting to read a non-existent file.

Example 10: FileReader for Resource Files


import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Example10 {
public static void main(String[] args) {
try (FileReader fileReader = new FileReader(
new InputStreamReader(Example10.class.getResourceAsStream("/resourceFile.txt")))) {
// File reading logic for resource files
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation: Shows how to use `FileReader` to read data from a resource file within the classpath.

Conclusion:


Mastering the FileReader class in Java is essential for efficient file handling. These ten examples provide a solid foundation for reading files in various scenarios, from simple text files to resource files and binary data. By incorporating these techniques into your Java applications, you'll be well-equipped to handle a wide range of file reading requirements.


Post a Comment

Previous Post Next Post