Question 10
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
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!