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

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);
}
}
这里哪儿不对呀?谢谢
...全文
8 点赞 收藏 3
写回复
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;
}
}
}
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告