Java FileReader error

Hi, I'm a beginner of Java language

It seems that my computer can't recognize FileReader at all (random classes don't work.) I entered exactly the same code on different computers and it worked I uninstalled the JDK and reinstalled it, but it 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)

This 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 gave me the wrong saying "no symbol found"

Looks like this filereadergg Java: 11: symbol not found: constructor FileReader (Java. Lang. string) location: class FileReader FileReader infile = new FileReader ("todolint. TXT");

There are five more mistakes I spent the whole day trying to find out the problem Please help me

Solution

This means that you are trying to use a constructor that does not exist Obviously, you're trying to enter a string into a constructor, but no constructor accepts only string values, but for Java io. FileReader is not Is there another class called "FileReader" in the same package (folder)? If so, line 8 should be

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

Replace Other options 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();
  }
}

Notice how to declare all packages explicitly without importing This should work anyway As you know, line 5 gets (a) the path where the program runs (I hope it is the same as the resource file) and (b) checks whether it is in the package and adds the required subfolders (but it looks like you are not, so you may not need it)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>