Java 文件读取器错误

发布于 2024-09-24 17:59:49 字数 858 浏览 3 评论 0原文

嗨,我是 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 技术交流群。

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

发布评论

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

评论(3

孤独患者 2024-10-01 17:59:49

这意味着您正在尝试使用不存在的构造函数。显然,您正在尝试将 String 输入到构造函数中,但没有构造函数接受 String 值,但事实并非如此对于java.io.FileReader。同一包(文件夹)中是否还有另一个名为“FileReader”的类?如果是这样,则应改为第 8 行

java.io.FileReader infile = new java.io.FileReader("todolist.txt");

。其他解决方案包括

public class FileReaderGG
{
  public static void main(String[] args) throws Exception
  {
    String pathName = System.getProperty("user.dir") + (FileReaderGG.class.getPackage() == null ? "" : "\\" + FileReaderGG.class.getPackage().getName().replace('.', '\\'));

    java.io.FileReader infile = new java.io.FileReader(pathName + "\\todolist.txt");

    java.util.Scanner indata = new java.util.Scanner(infile);

    while (indata.hasNextLine())
    {
      System.out.println(indata.nextLine());
    }
    infile.close();
  }
}

注意如何不进行导入以及如何显式声明所有包。无论如何,这应该有效。正如您所知,第 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 a String value, but that is not true for java.io.FileReader. Is there another class in the same package (folder) called "FileReader"? If so, line 8 should be

java.io.FileReader infile = new java.io.FileReader("todolist.txt");

instead. Other solutions include

public class FileReaderGG
{
  public static void main(String[] args) throws Exception
  {
    String pathName = System.getProperty("user.dir") + (FileReaderGG.class.getPackage() == null ? "" : "\\" + FileReaderGG.class.getPackage().getName().replace('.', '\\'));

    java.io.FileReader infile = new java.io.FileReader(pathName + "\\todolist.txt");

    java.util.Scanner indata = new java.util.Scanner(infile);

    while (indata.hasNextLine())
    {
      System.out.println(indata.nextLine());
    }
    infile.close();
  }
}

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)

锦欢 2024-10-01 17:59:49

我认为你的代码是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?

如果没有你 2024-10-01 17:59:49

我认为你必须导入更多,这就是我的意思:

import java.util.Scanner;
import java.util.Scanner.*;
import java.io.FileReader;
import java.io.FileReader.*;

你知道,当你

import java.util.Scanner;

只导入“Scanner”包,而不导入Scanner包中的其他包时。

I think you have to import more, here's what I mean:

import java.util.Scanner;
import java.util.Scanner.*;
import java.io.FileReader;
import java.io.FileReader.*;

You know that when you

import java.util.Scanner;

It only imports the "Scanner" package, but not other packages in the Scanner package.

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