BufferedWrite中:用void newLine()写一行行分隔符,分隔符由系统属性定义
BuffereReader:public String readLine():读一行文字,结果包含的内容的字符串,如果流的结尾已经到达,则为null
package day4; import java.io.*; public class WriteReadTest { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("write.txt"); BufferedWriter bw = new BufferedWriter(fw); for (int i = 0; i < 5; i++) { bw.write("hello"); bw.newLine(); } bw.flush(); FileReader fr = new FileReader("write.txt"); BufferedReader br = new BufferedReader(fr); String s; while ((s=br.readLine())!=null){ System.out.println(s); } br.close(); fr.close(); bw.close(); fw.close(); } }
用字符缓冲流复制文件
package day4; import java.io.*; public class WriteReadTest { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("write2.txt"); BufferedWriter bw = new BufferedWriter(fw); FileReader fr = new FileReader("write.txt"); BufferedReader br = new BufferedReader(fr); String s; while ((s=br.readLine())!=null){ bw.write(s); bw.newLine(); } br.close(); fr.close(); bw.close(); fw.close(); } }