LearnFrenzy
Sign in Take a free test

File Handling, IO and NIO · Find the Error · Question 10

Error: wrapping a Reader in an InputStreamReader Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) throws IOException {
        try (InputStreamReader r = new InputStreamReader(new FileReader("data.txt"))) {
            System.out.println(r.read());
        }
    }
}
  • A FileReader needs a charset
  • B InputStreamReader converts a byte stream (InputStream) to characters; a FileReader is already a Reader, so there is no such constructor – wrap a FileInputStream instead, or just use the FileReader Correct
  • C read() cannot be printed
  • D try-with-resources needs catch
Correct Answer: Option B

javac: "no suitable constructor found for InputStreamReader(FileReader)".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links