当前位置: 技术问答>java相关
怎么样循环读出文本文件里的所有数据?
来源: 互联网 发布时间:2015-06-12
本文导语: 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); } } 这里哪儿不对呀?谢谢...
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);
}
}
这里哪儿不对呀?谢谢
if(file.exists())
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr) ;
String str="";
while(!br.Next())
{
str=br.readLine();
out.println(str);
}
}
这里哪儿不对呀?谢谢
|
楼上的有点问题,这样:
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+"
");
str=br.readLine();
}
}
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+"
");
str=br.readLine();
}
}
|
/**
* 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;
}
}
}
* 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;
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。