/* Character Stream Reader Writer--filewriter character stream buffer: improve flow operation efficiency BufferedReader BufferedWriter */package Pack;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.filereader;import Java.io.FileWriter;import Java.io.linenumberreader;public class Main {public static void sys (Object obj) {System.out.println (obj); } public static void Main (string[] args) throws exception{//method1 ();//filewriter//method2 ();//filewri ter//method3 ();//filereader//method4 ();//Copy File//method5 ();//bufferedwriter//method6 ();//fil EReader//method7 ();//Copy File Method8 ();//linenumberreader add serial number function} public static void Method1 () throws Exc eption {FileWriter fw = new FileWriter ("E:\\1.txt"); Fw.write ("abc"); Writes a string to the stream Fw.flush (); Refreshes the data in the buffer in the Stream object Fw.close (); Off resource} public static void Method2 () throws Exception {FileWriter fw = new FileWriter ("E:\\1.txt", true);//No Cover the file Fw.write ("cc\r\nC "); NewLine Fw.flush (); Fw.close (); } public static void Method3 () throws Exception {FileReader fr = new FileReader ("E:\\1.txt"); /*int ch = 0; while ((Ch=fr.read ())!=-1) {sys (char) ch); }*//*char[] buf = new CHAR[9]; int ch = fr.read (BUF);//writes the array, CH is the number of Read SYS (new String (BUF));//output array */char[] buf = new Char[3]; int num = 0; while ((Num=fr.read (BUF))!=-1)//number is not 1 sys (new String (Buf,0,num));//from 0 to Num fr.close (); }/* Copy file */public static void Method4 () throws Exception {/*filereader fr = new FileReader ("E:\\1.txt"); FileWriter FW = new FileWriter ("E:\\2.txt"); int ch = 0; while ((Ch=fr.read ())!=-1) fw.write (CH); Fr.close (); Fw.close (); */FileReader FR = new FileReader ("E:\\1.txt"); FileWriter FW = new FileWriter ("E:\\2.txt"); char[] buf = new char[1024]; int len = 0; While((Len=fr.read (BUF))!=-1) Fw.write (Buf,0,len); Fw.close (); Fr.close (); The public static void Method5 () throws Exception {/* buffer is designed to increase efficiency by first having a stream */FileWriter FW = new FileWriter ("E:\ \1.txt "); BufferedWriter BUFW = new BufferedWriter (FW); Bufw.write ("KK"); Bufw.newline (); Write line break bufw.write ("GG"); Bufw.flush (); Bufw.close ();//close buffer is the stream object that closed buffer function} public static void Method6 () throws Exception {FileReader fr = new FileR Eader ("E:\\1.txt"); BufferedReader bufr = new BufferedReader (FR); String line = null; while ((Line=bufr.readline ())!=null)//Returns an empty representation read to the end of Sys (line); Bufr.close (); }/* Copy file */public static void Method7 () throws Exception {BufferedReader bufr = new BufferedReader (New FileR Eader ("E:\\1.txt")); BufferedWriter BUFW = new BufferedWriter (New FileWriter ("E:\\2.txt")); String line = null; while ((Line=bufr.readline ())! =NULL) Bufw.write (line); Bufr.close (); Bufw.close (); } public static void Method8 () throws Exception {FileReader fr = new FileReader ("E:\\1.txt"); LineNumberReader LNR = new LineNumberReader (FR); String line = null; while ((Line=lnr.readline ())!=null) sys (lnr.getlinenumber () + ":" +line); Lnr.close (); }}
Java character stream