Java 文件读取器错误
嗨,我是 Java 语言的初学者。
看来我的计算机根本无法识别 FileReader。(随机类也不起作用。)我在另一台计算机上键入了完全相同的代码,它起作用了。我卸载了JDK并重新安装了它,但仍然不起作用。我不知道该怎么办。
我的环境
三星上网本 N150 plus。 /// Windows 7 启动器/// java(1.6_21标准版) /// jGrasp(1.8)。
这是我的代码。
import java.io.*;
import java.util.*;
public class FileReaderGG
{
public static void main(String[] args)throws Exception
{
FileReader infile = new FileReader("todolist.txt");
Scanner indata = new Scanner(infile);
while (indata.hasNextLine())
{
System.out.println(indata.nextLine());
}
infile.close();
}
}
它给我错误说“找不到符号”
看起来像这样 FileReaderGG.java:11:找不到符号 符号:构造函数 FileReader(java.lang.String) 位置:FileReader类 FileReader infile = new FileReader("todolist.txt");
还有5个错误。我花了一整天的时间试图找出问题所在。 请帮帮我。
Hi I'm a beginner of Java lauguage.
It seems like my computer does not recognize FileReader at all.(Random class does not work either.) I typed the exact same code in a different computer and it worked. I uninstalled JDK and reinstalled it, but still doesn't work. I don't know what to do.
My environment
Samsung Netbook N150 plus. ///
windows 7 starter///
java(1.6_21 standard edition) ///
jGrasp(1.8).
Here is my code.
import java.io.*;
import java.util.*;
public class FileReaderGG
{
public static void main(String[] args)throws Exception
{
FileReader infile = new FileReader("todolist.txt");
Scanner indata = new Scanner(infile);
while (indata.hasNextLine())
{
System.out.println(indata.nextLine());
}
infile.close();
}
}
It gives me errors saying "cannot find symbol"
Looks like this
FileReaderGG.java:11: cannot find symbol
symbol : constructor FileReader(java.lang.String)
location: class FileReader
FileReader infile = new FileReader("todolist.txt");
5 more errors are there. I spent a whole day trying to figure out what the problem is.
Please help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这意味着您正在尝试使用不存在的构造函数。显然,您正在尝试将
String
输入到构造函数中,但没有构造函数只接受String
值,但事实并非如此对于java.io.FileReader。同一包(文件夹)中是否还有另一个名为“FileReader
”的类?如果是这样,则应改为第 8 行。其他解决方案包括
注意如何不进行导入以及如何显式声明所有包。无论如何,这应该有效。正如您所知,第 5 行获取 (A) 运行程序的路径(希望与资源文件相同),并 (B) 检查它是否在包中并添加所需的子文件夹(尽管如此,看来您不在其中,所以可能不需要)
It means that you are trying to use a constructor that isn't there. Apparently you are trying to input a
String
into the constructor, but there is no constructor that accepts just aString
value, but that is not true forjava.io.FileReader
. Is there another class in the same package (folder) called "FileReader
"? If so, line 8 should beinstead. Other solutions include
Note how no imports are made and all packages are explicitly declared. This should work no matter what. Just so you know, line 5 gets (A) the path from which the program is being run (hopefully the same as the resource file) and (B) checks if it is in a package and adds the needed sub-folders (though, it seems you aren't in any so it probably isn't needed)
我认为你的代码是100%正确的。至少对我来说它是有效的。你是从 IDE 还是从命令行编译这个程序?
I think your code is 100% right. Its working on my end at least. Are u compiling this program from IDE or from command line?
我认为你必须导入更多,这就是我的意思:
你知道,当你
只导入“Scanner”包,而不导入Scanner包中的其他包时。
I think you have to import more, here's what I mean:
You know that when you
It only imports the "Scanner" package, but not other packages in the Scanner package.