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:37: cannot find symbol
symbol : method FileReader(java.lang.String)
location: class java.io.BufferedReader
line = read.FileReader(newDoc);
Here is the code.
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...");
}
}
}
解决方案
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.