尝试编译程序时出错
我正在尝试运行从 Sun Java 站点获取的这段代码(我没有复制它,而是查看并编写它,因为它可以帮助我记住代码)。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharEx{
FileReader inputStream = null;
FileWriter outputStream = null;
public static void main(String args[]) throws IOException{
FileReader inputStream = null;
FileWriter outputStream = null;
try{
inputStream = FileReader("xanadu.txt");
outputStream = FileWriter("out.txt");
int c;
while ((c = inputStream.read()) != -1){
outputStream(c);
}
}
finally{
if(inputStream !=null){
inputStream.close();
}
if(outputStream !=null){
outputStream.close();
}
}
}
}
但我收到以下错误。
D:\Java>javac CharEx.java
CharEx.java:14: cannot find symbol
symbol : method FileReader(java.lang.String)
location: class CharEx
inputStream = FileReader("xanadu.txt");
^
CharEx.java:15: cannot find symbol
symbol : method FileWriter(java.lang.String)
location: class CharEx
outputStream = FileWriter("out.txt");
^
CharEx.java:18: cannot find symbol
symbol : method outputStream(int)
location: class CharEx
outputStream(c);
^
3 errors
从消息中我认为系统正在 java.lang
内寻找 FileReader
而它应该在 java.io.*
内寻找它: ((
有人可以帮我解决我出错的地方吗?
PS:我使用的是 JDK 1.5。
I'm trying to run this code taken from Sun Java site (I didn't copy it, Looked at it and wrote it as it would help me to remember the code).
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharEx{
FileReader inputStream = null;
FileWriter outputStream = null;
public static void main(String args[]) throws IOException{
FileReader inputStream = null;
FileWriter outputStream = null;
try{
inputStream = FileReader("xanadu.txt");
outputStream = FileWriter("out.txt");
int c;
while ((c = inputStream.read()) != -1){
outputStream(c);
}
}
finally{
if(inputStream !=null){
inputStream.close();
}
if(outputStream !=null){
outputStream.close();
}
}
}
}
But I'm getting follwing error.
D:\Java>javac CharEx.java
CharEx.java:14: cannot find symbol
symbol : method FileReader(java.lang.String)
location: class CharEx
inputStream = FileReader("xanadu.txt");
^
CharEx.java:15: cannot find symbol
symbol : method FileWriter(java.lang.String)
location: class CharEx
outputStream = FileWriter("out.txt");
^
CharEx.java:18: cannot find symbol
symbol : method outputStream(int)
location: class CharEx
outputStream(c);
^
3 errors
From the message I think that the system is looking for FileReader
inside java.lang
whereas it should look for it inside java.io.*
:((
Can someone help me where I'm getting wrong?
PS: I'm on JDK 1.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试实例化一个
FileReader
和一个FileWriter
(即创建这些类型的对象)。为此,您需要使用
new
关键字:通过省略
new
,代码看起来像方法调用,因此编译器会查找名为FileReader 的方法
(和FileWriter
),但没有找到它,它用一种有点奇怪但出奇清晰的语言告诉你。提示:“符号”是编译器所说的“名称”。该名称可以是类、方法、变量……在检查“symbol:”行时可以找到确切的问题。它告诉您编译器寻找名为
FileReader
的方法,该方法采用String
参数:You're trying to instantiate a
FileReader
and aFileWriter
(i.e. create objects of those types).To do that you need to use the
new
keyword:By leaving out the
new
the code looks like a method call, so the compiler looks for a method namedFileReader
(andFileWriter
) and doesn't find it, which it tells you in a somewhat strange, but surprisingly clear language.Hint: "symbol" is what a compiler calls a "name". That name can be of a class, method, variables, ... The exact problem can be found when checking the "symbol: "line. It tells you that the compiler looks for a method called
FileReader
that takes aString
parameter:初始化读取器和写入器时缺少
new
关键字。您还缺少此行中的某些内容:
您想在那里写入输出流吗?那么你应该尝试这个:
You are missing the
new
keyword when initializing the reader and writer.You are also missing something on this line:
Do you want to write to the output stream there? Then you should try this: