一、读取文件内容的三种方式
1、文件的读取
主要介绍两种常用的读取方法。按行读取和按字符块读取。
1.1 一次读取一行作为输入
//按行读取文件内容
public static String Read1(String infile) throws Exception //infile="data/in.txt"
{
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(infile));
String data = br.readLine();
while(data != null)
{
sb.append(data+"\n");
data = br.readLine();
}
br.close();
return sb.toString();
}
1.2 一次读取指定大小的字符块
关于一次读取一个字符的方法就不讲了,感觉这种方法效率太低了!
//以字符块读取文件
public static String Read2(String infile) throws Exception
{
StringBuffer sb = new StringBuffer();
File file = new File(infile);
FileInputStream fis = new FileInputStream(file);
byte buffer[] = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) != -1)
{
sb.append(new String(buffer, 0