怎么样循环读出文本文件里的所有数据?

AaddB 2002-05-30 06:42:03
File file = new File(request.getRealPath("test")+"/byo.txt");
if(file.exists())
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr) ;
String str="";
while(!br.Next())
{
str=br.readLine();
out.println(str);
}
}
这里哪儿不对呀?谢谢
...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
希偌 2002-05-30
  • 打赏
  • 举报
回复
楼上的有点问题,这样:
File file = new File(request.getRealPath("test")+"/byo.txt");
if(file.exists())
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str=br.readLine();
while(str!=null)
{
out.println(str+"<br>");
str=br.readLine();
}
}
希偌 2002-05-30
  • 打赏
  • 举报
回复
File file = new File(request.getRealPath("test")+"/byo.txt");
if(file.exists())
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr) ;
String str="";
while(str!=null)
{
str=br.readLine();
out.println(str+"<br>");
}
}
%>
Andrawu 2002-05-30
  • 打赏
  • 举报
回复
/**
* Returns the content of the file in a String.
* If an error occurs, like if the file does not exists, null is returned.
*/
public String getContent() {
String content = "";
File file = new File(fileName);
if (!file.exists()) {
setErrorMessage("Error: The file '" + fileName + "' does not exists.");
return null;
}
else if (file != null) {
try {
// Create an BufferedReader so we can read a line at the time.
BufferedReader reader = new BufferedReader(new FileReader(file));
String inLine = reader.readLine();
while (inLine != null) {
if (inLine.length() + 1 > columns)
columns = inLine.length() + 1;
content += (inLine + System.getProperty("line.separator"));
inLine = reader.readLine();
rowCount++;
}
return content;
}
catch (IOException e) {
setErrorMessage("Error reading the file: " + e.getMessage());
return null;
}
}
else {
setErrorMessage("Unknown error!");
return null;
}
}
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧