java 1.4.2 控制面板,Java 1.4.2 - 读取文件

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值