Java 1.4.2 - 读取文件

时间:2013-08-09 13:48:04

标签: java file-io

我正在尝试读取一个简单文件,然后读取用户应该选择的文件。我继续收到以下错误:

  

Readzilla.java:37:找不到符号

     

symbol:方法FileReader(java.lang.String)

     

location:class java.io.BufferedReader

     

line = read.FileReader(newDoc);

这是代码。

import java.io.*;

    public class Readzilla
    {
        public static void main(String[] args) throws IOException
        {
            String line;
            BufferedReader read;
            // BufferedReader "read" reads the file
            BufferedReader in;
            // BufferedReader "in" reads the input sent by the user  
            String loop;
            // "loop" decides whether another document should be read

            in = new BufferedReader(new InputStreamReader(System.in));
            read = new BufferedReader(new FileReader("message.txt"));
            line = read.readLine();

            while(line != null)
            {
                System.out.println(line);
                line = read.readLine();
            }

            // read another document
            System.out.println("Would you like to read another document? (Y/N)");
            loop = in.readLine();
            loop = loop.toUpperCase();

            if (loop == "Y")
            {
                do
                {
                    System.out.println("What file (.txt) would you like to read?");
                    String newDoc = in.readLine();
                    // newDoc reads a text file of the user's choosing
                    line = read.FileReader(newDoc);
                    //  ^ This line constantly gives errors
                    System.out.println("Reading...");
                    line = read.readLine();

                    while(line != null)
                    {
                        System.out.println(line);
                        line = read.readLine();
                    }

                    // read another document
                    System.out.println("Would you like to read another document? (Y/N)");
                    loop = in.readLine();
                }
                while (loop == "Y");    
            }
            else
            {
                System.out.println("Closing Program...");
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

你的问题就在这一行:

line = read.FileReader(newDoc);

FileReader上没有名为BufferedReader的方法,这是编译器解释该行的方式。 FileReader本身就是一个类,看起来你正在尝试打开一个新文件进行阅读。因此,你想说的是:

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

之后,你想要替换

line = read.readLine();

line = doc.readLine()

因为这是您从BufferedReader doc引用的文档中读取的方式。

此外,你在这里遇到了问题(我看到的两倍):

loop == "Y"

在Java中,==只是引用相等 。你绝对想要价值平等,所以说:

"Y".equals(loop);

这是一个常见的错误; ==作为参考平等只是一个糟糕的设计决策IMO。

答案 1 :(得分:1)

只有在使用“new”创建对象时,才能从对象调用构造函数。你只想说'line = read.readLine();'对于问题线。

答案 2 :(得分:0)

BufferedReader课程没有名为FileReader的方法。

您可以在documentation

中看到它

在Java 1.4.2中读取文件的一种方法是:

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");
}