如何读取文件的内容为string

这儿提供一个java方法:

    public String readFileAsString(String filePath)
throws java.io.IOException {
StringBuffer fileData = new StringBuffer("");
BufferedReader reader = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}

你可能感兴趣的:(读取,文件,string,java,读取,文件,string,java)