Java 1.4.2 - Reading Files

后端 未结 3 1257
南笙
南笙 2020-12-22 06:58

I am trying to read a simple file and then a file which the user is supposed to select. I keep on getting the following error though:

Readzilla.java:3

相关标签:
3条回答
  • 2020-12-22 07:19

    Your problem is this line:

    line = read.FileReader(newDoc);
    

    There is no method named FileReader on the class BufferedReader, which is how the compiler is interpreting that line. FileReader is itself a class, and it looks like you're trying to open a new file for reading. Thus, you'd want to say something like:

    BufferedReader doc = new BufferedReader(new FileReader(newDoc));
    

    After that, you'd want to replace

    line = read.readLine();
    

    with

    line = doc.readLine()
    

    because that's how you'd read from the document referenced by the BufferedReader doc.

    Additionally, you have a problem here (twice that I see):

    loop == "Y"
    

    In Java, == is reference equality only. You absolutely want value equality here so say:

    "Y".equals(loop);
    

    This is a common mistake; == as reference equality only was a poor design decision IMO.

    0 讨论(0)
  • 2020-12-22 07:25

    You cannot invoke a constructor from an object, only when you create the object with 'new'. You would just say 'line = read.readLine();' for the problem line.

    0 讨论(0)
  • 2020-12-22 07:26

    BufferedReader class doesn't have a method called FileReader.

    You can see it in the documentation.

    One way of reading a file in Java 1.4.2 is:

    try
    {
        String line;
        File file = new File(file);
        BufferedReader inFile = new BufferedReader(new FileReader(file));
        while((line = inFile.readLine()) != null)
        {
                System.out.println(line)
        }
        inFile.close();
    }
    catch (IOException e)
    {
        System.out.println("problem with file");
    }
    
    0 讨论(0)
提交回复
热议问题