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; }
相关文章推荐
- 8.9 编写函数打开文件用于输入,将文件内容读入 string 类型的 vector 容器,每一行存储为该容器对象 的一个元素。8.10 重写上面的程序,把文件中的每个单词存储为 容器的一个元素
- 一个简单的写入文件内容的小demo
- C++ 读文件 将文件内容读入到字符串string中
- C++ Demo 001: 读取一个从命令行指定的文件,输出文件内容到显示器,并且给每一行输出添加一个行号
- 将文件每一行读入到string类型的vector 对应一个元素中
- 将一个文件中的内容存储在一个String字符串中
- 把整个文本文件内容读到一个string里
- 字符串写入文件 C++ 读文件 将文件内容读入到字符串string中
- 如何用java读取一个txt 文件内的内容并把它赋值与String里?
- C++ 读文件 将文件内容读入到字符串string中的方法
- 读取一个文件中的内容到String中
- shell读取一个文件内容到变量并逐一赋值
- 从文件中读入文本,文本内容为几个字符串,用逗号间隔,将其中同时含有字母和数字的字符串去除,留下仅仅只包含字母或数字的字符串,然后进行排序
- Ant Mail:用一个Template文件作为邮件内容
- ubuntu 像另外一个文件中添加内容
- 典型的Top K算法_找出一个数组里面前K个最大数...或找出1亿个浮点数中最大的10000个...一个文本文件,找出前10个经常出现的词,但这次文件比较长,说是上亿行或十亿行,总之无法一次读入内存,
- c++ 读取文件内容存到string中
- 合并一个文件夹下多个文件内容
- C++ 将一个文件读入数组再读出数组的方法