1.带缓存的字符输入输出流
1 package com.xia; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 9 public class test { 10 11 public static void main(String[] args) { 12 13 try { 14 15 File f = new File ("d:\\233333.txt"); 16 17 //带缓存 18 //writer接口的实现类 19 20 FileWriter fw = new FileWriter(f,true); 21 22 //缓存写入类,构造时需要传入writer实例. 23 BufferedWriter bw = new BufferedWriter(fw); 24 25 bw.write("\n第一句"); 26 27 //自动管理缓存 28 //自动写入:1.缓存满了;2.程序缓存关闭之前; 29 30 bw.flush(); //主动清空缓存 写入数据. 31 32 bw.write("\n第二句"); 33 34 bw.close(); 35 36 fw.close(); 37 38 System.out.println("写入完成!"); 39 40 //缓存读 41 42 FileReader fr = new FileReader(f); 43 44 BufferedReader br = new BufferedReader(fr); 45 46 //先第一次读 47 // String str = br.readLine(); 48 // 49 // while (str != null) 50 // { 51 // System.out.println(str); 52 // 53 // str = br.readLine(); 54 // } 55 56 while (true) 57 { 58 String str = br.readLine(); 59 60 if(str == null) 61 { 62 break; 63 } 64 65 System.out.println(str); 66 } 67 68 } catch (Exception e) { 69 70 e.printStackTrace(); 71 } 72 } 73 }
2.多进程