尝试编译程序时出错

发布于 2024-11-18 20:21:47 字数 1704 浏览 4 评论 0原文

我正在尝试运行从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音栖息无 2024-11-25 20:21:47

您正在尝试实例化一个 FileReader 和一个 FileWriter (即创建这些类型的对象)。

为此,您需要使用 new 关键字:

inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("out.txt");

通过省略 new,代码看起来像方法调用,因此编译器会查找名为 FileReader 的方法 (和 FileWriter),但没有找到它,它用一种有点奇怪但出奇清晰的语言告诉你。

提示:“符号”是编译器所说的“名称”。该名称可以是类、方法、变量……在检查“symbol:”行时可以找到确切的问题。它告诉您编译器寻找名为 FileReader方法,该方法采用 String 参数:

CharEx.java:14: cannot find symbol
symbol  : method FileReader(java.lang.String)

You're trying to instantiate a FileReader and a FileWriter (i.e. create objects of those types).

To do that you need to use the new keyword:

inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("out.txt");

By leaving out the new the code looks like a method call, so the compiler looks for a method named FileReader (and FileWriter) 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 a String parameter:

CharEx.java:14: cannot find symbol
symbol  : method FileReader(java.lang.String)
浪菊怪哟 2024-11-25 20:21:47

初始化读取器和写入器时缺少 new 关键字。

inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("out.txt");

您还缺少此行中的某些内容:

outputStream(c);

您想在那里写入输出流吗?那么你应该尝试这个:

outputStream.write(c);

You are missing the new keyword when initializing the reader and writer.

inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("out.txt");

You are also missing something on this line:

outputStream(c);

Do you want to write to the output stream there? Then you should try this:

outputStream.write(c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文