- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- class BufferedReaderDemo
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr = new FileReader("BufferedReaderDemo.java");
- FileReader fr1 = new FileReader("BufferedReaderDemo.java");
- BufferedReader bufr = new BufferedReader(fr);
- FileWriter fw = new FileWriter("buf.txt");
- FileWriter fw1 = new FileWriter("buf1.txt");
- BufferedWriter bufw = new BufferedWriter(fw);
- int i=-1;
- String line = null;
- long stact = System.currentTimeMillis();
- while((i=fr1.read())!=-1)//用read:它读一个写一个,读取流和写入流来回的进出内存比较耗时
- {
- fw1.write(i);
- fw1.flush();
- }
- System.out.println(System.currentTimeMillis() - stact);
- stact = System.currentTimeMillis();
- while((line=bufr.readLine())!=null)//用readLine:读一堆写一堆,读取流读够了,写入流在写,读取流和写入流交换不频繁。
- {
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- System.out.println(System.currentTimeMillis() - stact);
- fw1.close();
- bufr.close();
- bufw.close();
复制代码
|