首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

fileContent += br.readLine();这样写对吗?解决方案

2012-01-02 
fileContent + br.readLine()这样写对吗??fileContent + br.readLine()fileContent是一个字符串,用一

fileContent += br.readLine();这样写对吗??
fileContent += br.readLine();
fileContent是一个字符串,用一个while循环将读出的内容全部放进fileContent这个字符串中。

[解决办法]

Java code
    public String fileToString(String fileName) {    StringBuilder fileContent = new StringBuilder();    File file = new File(fileName);    BufferedReader br = null;    try {        br = new BufferedReader(new FileReader(file));        String string;        while ((string = br.readLine()) != null) {        fileContent.append(string);        fileContent.append("\n");        }    } catch (IOException e) {        throw new RuntimeException(e);    } finally {        if (br != null)        try {            br.close();        } catch (IOException e) {            throw new RuntimeException(e);        }    }    return fileContent.toString();    }
[解决办法]
这个帖子和你要做的是一回事如何用java 读取一个txt 文件内的内容并把它赋值与String 里?
[解决办法]
Java code
public String fileToString(String fileName) {
StringBuilder fileContent = new StringBuilder();
File file = new File(fileName);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));

String string;
while ((string = br.readLine()) != null) {
fileContent.append(string);
fileContent.append("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

return fileContent.toString();
}

对的
[解决办法]
按行读取文件内容,然后做你想做的事。
楼主想把所有内容读出来然后放到一个字符串中,2楼的代码能说明问题了,
然后对于你的代码:
Java code
fileContent += br.readLine(); 

热点排行
Bad Request.