0
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

Selva
  • 546
  • 5
  • 12
  • 34

3 Answers3

3

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)

fge
  • 119,121
  • 33
  • 254
  • 329
2

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:

  • You should use a try-with-resources statement to close the resource automatically
  • I wouldn't name a 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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Selva: "I am getting error" doesn't tell us anything about *what* the error is. If it's because the compiler can't find `StandardCharsets`, that's because you need an import for it (e.g. `import java.nio.charset.StandardCharsets;`.) – Jon Skeet Feb 26 '14 at 13:32
  • @JohnSkeet gelp me to find sollution – Selva Feb 26 '14 at 13:33
  • @Selva: How can I help you to find a solution when you won't say what the error is? – Jon Skeet Feb 26 '14 at 13:33
  • @JohnSkeet while import 'java.nio.charset.StandardCharsets;' this package it tells: The import java.nio.charset.StandardCharsets cannot be resolved – Selva Feb 26 '14 at 13:38
  • sir no sir i will compile my program using 'javac -source 5 filename.java' only – Selva Feb 26 '14 at 14:01
  • @Selva: Why are you specifying -source 5? – Jon Skeet Feb 26 '14 at 14:04
  • because it will defaultly take jdk 1.4, so sorry to say i will specify 'javac -source 7 filename.java' sir....... – Selva Feb 26 '14 at 14:06
  • @Selva: Why have you got it set up to default to JDK 1.4 source? I don't even know *how* you'd set it up that way... and certainly I can't see any reason you'd *want* it set up that way. A stock install of JDK 7 will default to a source level of 7. – Jon Skeet Feb 26 '14 at 14:11
  • sir no if i compile using this way only i can use the jdk 7 other wise it will use jdk 4 only. the best example is for each loop it will works after 5 version only if i compile file normally it wont work in my system for for-each loop – Selva Feb 26 '14 at 14:19
  • @Selva: Well I don't know how you've set it up that way. That's very odd indeed. (Note that it's not actually using the JDK 1.4; it's just compiling with the language features from Java 1.4.) – Jon Skeet Feb 26 '14 at 14:26
1

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(is))); and it should work.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99