您的位置:首页 > 其它

Demo:把文件内容读入一个String

2013-10-29 21:14 204 查看
public static String readFileToString() throws IOException {
//		File dirs = new File(".");
StringBuffer fileData = new StringBuffer(1000);//Constructs a string buffer with no characters in it and the specified initial capacity
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
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();
String returnStr = fileData.toString();
System.out.println(returnStr);
return returnStr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐