本文介绍了Java 1.4.2 - 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图读取一个简单的文件,然后读取用户应该选择的文件。尽管如此,我一直在收到以下错误:
这里是代码。
import java.io. *;
public class Readzilla
{
public static void main(String [] args)throws IOException
{
String line;
BufferedReader读取;
// BufferedReaderread读取文件
BufferedReader in;
// BufferedReaderin读取用户发送的输入
字符串循环;
//loop决定是否读取另一个文档
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();
//读取另一个文档
System.out.println(你想读另一个文档吗?(是/否));
loop = in.readLine();
loop = loop.toUpperCase();
if(loop ==Y)
{
do
{
System.out.println(What file(.txt)would你喜欢阅读?);
String newDoc = in.readLine();
// newDoc读取用户选择的文本文件
line = read.FileReader(newDoc);
// ^这行不断给出错误
System.out.println(Reading ...);
line = read.readLine();
while(line!= null)
{
System.out.println(line);
line = read.readLine();
//读取另一个文档
System.out.println(你想读另一个文档吗?(是/否));
loop = in.readLine();
}
while(loop ==Y);
}
else
{
System.out.println(Closing Program ...);
$ div class =h2_lin>解决方案
line = read.FileReader(newDoc);
没有名为关于,这是编译器如何解释该行。 FileReader
本身就是一个类,它看起来像是在尝试打开一个新的文件来阅读。因此,您可能想要这样说:
BufferedReader doc = new BufferedReader(new FileReader(newDoc));
之后,您需要替换
line = read.readLine();
与
line = doc.readLine()
因为这是您从文档中读取引用由 BufferedReader doc
。
另外,这里有一个问题(我看到两次) p>
loop ==Y
在Java中, ==
是 reference 仅 。在这里你绝对需要价值平等,所以说:
$ $ p $ $ $ $ $ $ $.equals(loop);
这是一个常见的错误; ==
作为参考平等只是一个糟糕的设计决定国际海事组织。
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:
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.
这篇关于Java 1.4.2 - 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!