Java 构造函数 InputStreamReader(File) 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22042243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The constructor InputStreamReader(File) is undefined
提问by Selva
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
Streams.java 中的错误(第 7 行)BufferedReader br = new BufferedReader(new InputStreamReader(is)); ^^^^^^^^^^^^^^^^^^^^^^^^^
构造函数 InputStreamReader(File) 未定义
kindly help me out this problem i am using java 1.7.0_51version, OS linux Deepin
请帮我解决这个问题,我使用的是java1.7.0_51版本,OS linux Deepin
Thanks in advance
提前致谢
回答by Antoniossss
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(is)));and it should work.
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(is)));它应该工作。
回答by Jon Skeet
Yes, it's quite right. Look at the documentation for InputStreamReaderand you won't find a constructor taking a Fileparameter.
是的,完全正确。查看文档InputStreamReader,您将找不到带File参数的构造函数。
Instead, you should construct a FileInputStreamto read from the file, and pass thatto the constructor of the InputStreamReader. You should also specify the encoding you want to use, otherwise it'll use the platform default encoding.
相反,你应该建立一个FileInputStream从文件中读取,并通过该给的构造InputStreamReader。您还应该指定要使用的编码,否则它将使用平台默认编码。
Also note:
另请注意:
- You should use a try-with-resources statement to close the resource automatically
- I wouldn't name a
Filevariableis- that sounds more like you'd expect it to be anInputStream.
- 您应该使用 try-with-resources 语句自动关闭资源
- 我不会命名一个
File变量is- 这听起来更像是你期望它是一个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 FilesAPI as per fge's answer.)
(或者Files按照 fge 的回答使用API。)
回答by fge
You use Java 7?
你使用 Java 7 吗?
Then:
然后:
Files.newBufferedReader(Paths.get(System.getProperty("home.dir")
.resolve("java/in.txt")), StandardCharsets.UTF_8);
You use Java 7? Drop Fileentirely. See Files, Paths, FileSystems, etc etc.
你使用 Java 7 吗?File彻底放下。见Files,Paths,FileSystems,等等等等。
(edit: and use the try-with-resources statement; see @JonSkeet's answer for more details)
(编辑:并使用 try-with-resources 语句;有关更多详细信息,请参阅 @JonSkeet 的回答)

