【发布时间】:2014-07-30 04:28:51
【问题描述】:
这是我的代码不起作用:
try
{
while ((line1 = br.readLine()).substring(6).equals(name))
{
text = text + line1;
//text = text + '\n';
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
我要做的是从文本文件中读取文本:
Name: Thailand.jpg.
Brief: This is Pattaya
Info: Nice city you know
Name: Austria.jpg
Brief: This is Austria
Info: Schwarzenegger was born here
现在我只想设置“这是奥地利”文本。但不能。
谢谢!
【问题讨论】:
-
所以您只想从文件中获取字符串“This is Austria”?
-
这一行
while ((line1 = br.readLine()).substring(6).equals(name)) {是一团糟。分成可读的逻辑。还有什么是name?为什么substring(6)? -
名字是奥地利。我需要来自奥地利的文本。
-
那么你的问题是什么?注意,您需要先测试
readLine()的结果是否为空,然后再对其进行任何其他操作。此代码将在文件末尾抛出NullPointerException。
标签: java bufferedreader