代码如下:
public class MyWork { public static void main(String[] args) throws IOException{ BufferedReader fr = new BufferedReader(new FileReader("myfile/abc.txt")); BufferedWriter fw = new BufferedWriter(new FileWriter("myfile/test.txt")); String line; while ((line = fr.readLine()) != null) { fw.write(line); fw.newLine(); fw.flush(); } fr.close(); fw.close(); //这一段是测试代码 RandomAccessFile rf = new RandomAccessFile("myfile/abc.txt", "r"); int len; while ((len = rf.read()) != -1) { System.out.print((char)len); } System.out.println(); rf.seek(0); while ((len = rf.read()) != -1) { System.out.print((char)len); } } }
备注:字符缓冲流有自己特有的方法,也可以用普通的read()和write()方法。