//以前,我们读写文件,是从硬盘--读到内存----再写到硬盘里的
//Buffered(缓冲区)是:从硬盘---读到缓冲区----再读到内存里---再写到缓冲区里----再写到硬盘里.只是起到一个过度的作用,只是是觉得很麻烦,呵呵.
//是这样的,因为,从硬盘直接读取到内存很慢,而从缓冲区读到内存很快.
//或许你又会问,不还是要从硬盘读到缓冲区么?不也慢么?是这样的,缓冲区,一次性就从硬盘读取8192个字节,所以....
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo12 {
public static void main(String[] args) throws IOException {
demo1();
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
String line = null;
while((line = br.readLine()) != null) {
bw.write(line);
bw.flush();
bw.newLine();
}
br.close(); //看到你的代码没有关流,记得要关流哦,很重要的,呵呵,因为会占用系统资源的
bw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
char[] buffer = new char[1024];
int len;
while((len = fr.read(buffer)) != -1)
fw.write(buffer,0, len);
fr.close();
fw.close();
}
} |