The constructor InputStreamReader(File) is undefined

Selva picture Selva · Feb 26, 2014 · Viewed 15.5k times · Source
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

Answer

fge picture fge · Feb 26, 2014

You use Java 7?

Then:

Files.newBufferedReader(Paths.get(System.getProperty("home.dir")
    .resolve("java/in.txt")), StandardCharsets.UTF_8);

You use Java 7? Drop File entirely. See Files, Paths, FileSystems, etc etc.

(edit: and use the try-with-resources statement; see @JonSkeet's answer for more details)