日期:2014-05-20 浏览次数:21129 次
package test;
import java.io.*;
import java.util.*;
public class FileReadWrite {
/**
* 描述:readFile
*
* @param dir
* @return
* @throws IOException
* @CreateOn Jul 28, 2008 2:16:23 PM
* @author chun_chang
*/
public StringBuffer readFile(String dir) throws IOException {
StringBuffer buffer = null;
try {
buffer = new StringBuffer();
InputStream fis = new FileInputStream(dir);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = null;// 用来保存每行读取的内容
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("/n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
System.out.println(buffer); // 将读到 buffer 中的内容写出来
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return buffer;
}