import java.io.*;
public class Streams {
public static void main(String[] args) {
File homedir = new File(System.getProperty("user.home"));
File is = new File(homedir, "java/in.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int value = 0;
while ((value=br.read())!=-1) {
char c = (char) value;
System.out.println(c);
}
}
}
while compiling the above program i am getting error like this
ERROR in Streams.java (at line 7) BufferedReader br = new BufferedReader(new InputStreamReader(is)); ^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor InputStreamReader(File) is undefined
kindly help me out this problem i am using java 1.7.0_51
version, OS linux Deepin
Thanks in advance
Yes, it's quite right. Look at the documentation for InputStreamReader
and you won't find a constructor taking a File
parameter.
Instead, you should construct a FileInputStream
to read from the file, and pass that to the constructor of the InputStreamReader
. You should also specify the encoding you want to use, otherwise it'll use the platform default encoding.
Also note:
File
variable is
- that sounds more like you'd expect it to be an InputStream
.So for example:
File file = new File(homedir, "java/in.txt");
try (BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), StandardCharsets.UTF_8))) {
int value = 0;
while ((value = br.read()) != -1) {
char c = (char) value;
System.out.println(c);
}
}
(Or use the Files
API as per fge's answer.)
See more on this question at Stackoverflow