------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Dark Horse programmer--21, character stream filereader,filewriter,bufferedwriter,bufferedreader, decorative design mode, LineNumberReader
/*
Buffer technology for character streams:
For some large files, if you read a word characters write a character, read and write operations are too frequent.
In order to solve this problem, buffer technology was introduced.
Buffer technology is based on the flow (must have a flow first) to enhance the flow of data operational efficiency.
It is like drinking water on the faucet, originally a drop of a drop of drink, but now with a cup is loaded to drink, more efficient.
*/
Import java.io.*;class ioliou7{public static void Main (string[] args) {Filewrit ER fw=null; BufferedWriter Bfw=null; try {fw=new FileWriter ("F:\\yuyuyu.txt"); Bfw=new BufferedWriter (FW); Pass the write stream object to the buffer constructor Bfw.write ("Kamen Knight No. 01! "); In fact, the characters are written into the stream or FW calls the underlying resource implementation of the Bfw.newline (); NewLine Bfw.write ("Kamen Rider No. 02! "); Bfw.flush (); This is still to be refreshed before the data that exists stream is passed down the file} catch (IOException e) { throw new RuntimeException ("Incorrect write operation"); } finally {try { if (bfw!=null) { Bfw.close (); Closes the stream,/* Bfw.close (), essentially closes the stream or the FW underlying calls the close () method, so you don't have to write Fw.close () ; */}} catch (IOException e) {throw new Run Timeexception ("Bfw.close (); The problem throws an exception"); }}} public static void Soc (Object obj) {System.out.println (ob j); }}
—————— Split Line ——————
/*
Read buffer technology for character stream
BufferedReader
*/
Import Java.io.*;class ioliou8{publicstatic void Main (string[] args) {FileReader f R=null; BufferedReader Bfr=null; try {fr=new FileReader ("E:\\javawenjian\\ioliou8.java"); Bfr=new BufferedReader (FR); String S=null; while ((S=bfr.readline ())!=null)/* ReadLine () a line of reads, without any line terminator, returns null if no data is read. But do not return carriage returns, so the output to print or write when you remember to wrap the line. ReadLine reads the data essentially or calls the Read method one reading of the */{SOC (s); }} catch (IOException e) { throw new RuntimeException ("problem with read operation"); } finally {try { if (bfr!=null) bfr.close (); } catch (IOException e) {throw new Runtime Exception ("read operation problem"); }}} public static void Soc (Object obj) {System.out.println (ob j); }}
———————— Split Line ————
/*
Character streams use buffers to copy operation files
Copy the E:\\javawenjian\\ioliou8.java to the D drive.
*/
Import java.io.*;class ioliou9{public static void Main (string[] args) {FileWriter fw=null; FileReader Fr=null; BufferedWriter Bfw=null; BufferedReader Bfr=null; try {fw=new FileWriter ("D:\\ioliou8_copy.java");//create character stream write Object Fr=new FileRead ER ("E:\\javawenjian\\ioliou8.java");//build character stream read Object Bfw=new bufferedwriter (fw);//create character stream write buffer Bfr=new BufferedReader (FR);//build character stream read buffer String s= ""; while ((S=bfr.readline ())!=null) {bfw.write (s); Bfw.newline ();//newline} catch (Ioexce Ption e) {throw new RuntimeException ("Problem with copy operation"); } finally {try { if (bfw!=null) bfw.close (); } catch (IOException e) {thro W New RuntimeException ("bfw.close (); problem"); } try {if (bfr!=null) Bfr.close (); } catch (IOException E2) {throw new Runtimeexceptio N ("bfr.close (); problem"); }}} public static void Soc (Object obj) {S Ystem.out.println (obj); }}
———————— Split Line ——————
/*
Build a bufferedreader of your own
This design pattern is also called the decorative design pattern,
Customizing a class to pass existing objects in,
It then reinforces the existing functionality.
*/
Import Java.io.*;class Mybufferedreader {private FileReader fr=null; Mybufferedreader (FileReader fr) {this.fr=fr; } stringbuffer sb=new StringBuffer (); Public String ReadLine () throws IOException {char ch= '; int i=0; while ((I=fr.read ())!=-1) {if (i== ' \ R ') continue; if (i== ' \ n ') return sb.tostring (); Ch= (char) i; Sb.append (CH); } if (Sb!=null) return sb.tostring (); return null; /* Sb.tostring () returns a string inside the container, do not write the return value of the time must not be directly written to return SB SB is Stringbuff ER type of container! */} public void Close () throws IOException {fr.close (); } public static void Soc (Object obj) {System.out.println (obj); }}class ioliou10{public static void Main (string[] args) {Mybufferedreader BFR =null; FileReader Fr=null; try {fr=new FileReader ("F:\\yuyuyu.txt"); Bfr=new Mybufferedreader (FR); String S=bfr.readline (); Soc (s); } catch (IOException e) {throw new RuntimeException ("Operation exception "); } finally {try { if (bfr!=null) bfr.close (); } catch (IOException e) {throw new Runt ImeException ("Bfr.close (); a problem"); }}} public static void Soc (Object obj) {Sys Tem.out.println (obj); }}
—————— Split Line ————
/*
Description of Decoration class: (Decorative design mode)
You typically customize a class, pass in existing objects, and then add functionality that you already have.
Decorative and decorated classes have the same function, but the former is more powerful.
Therefore, the general adornment class and the adornment class are in the same individual system,
And the adornment class remembers to overwrite the abstract method of the parent class,
Note Whether you want to throw an exception.
*/
Import Java.io.*;class Mybufferedreader extends reader{private FileReader fr=null; Mybufferedreader (FileReader fr) {this.fr=fr; } stringbuffer sb=new StringBuffer (); Public String ReadLine () throws IOException {char ch= '; int i=0; while ((I=fr.read ())!=-1) {if (i== ' \ R ') continue; if (i== ' \ n ') return sb.tostring (); Ch= (char) i; Sb.append (CH); } if (Sb!=null) return sb.tostring (); return null; } public void Close () throws IOException {fr.close (); } public static void Soc (Object obj) {System.out.println (obj); }//Next remember to overwrite the abstract method in the reader class public int read (char[] cbuf,int off,int len) throws IOException {return fr.read (Cbuf,off,len); }}class ioliou11{public static void Main (string[] args) {Mybufferedreader bfr =nul L FileReader Fr=null; try {fr=new FileReader ("F:\\yuyuyu.txt"); Bfr=new Mybufferedreader (FR); String S=bfr.readline (); Soc (s); } catch (IOException e) {throw new RuntimeException ("Operation exception "); } finally {try { if (bfr!=null) bfr.close (); } catch (IOException e) {throw new RuntimeException ("bfr.close (); problem"); }}} public static void Soc (Object obj) {Sys Tem.out.println (obj); }}
—————— Split Line ——————
/*
Applications of the LineNumberReader class:
This class is a subclass of the BufferedReader class
*/
Import java.io.*;class ioliou12{public static void Main (string[] args) {FileReader Fr=null; LineNumberReader Lnr=null; try {fr=new FileReader ("Ioliou12.java"); Create a read stream object lnr= new LineNumberReader (FR); String s= ""; Lnr.setlinenumber (123); Sets the line number to start at the back row of 123 while ((S=lnr.readline ())!=null) {Soc (LNR. Getlinenumber () + ":" +s); The Getlinenumber method is used to get the line number}} catch (Ioexc Eption e) {throw new RuntimeException ("problem with read operation"); } finally {try { if (inr!=null) lnr.close (); } catch (IOException E2) {throw New RuntimeException ("Lnr.close (); the problem"); }}} public static void Soc (Object ob j) {System.out.println (obj); }}
The results of the compilation run are shown in 1:
Figure 1
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse programmer--21, character stream filereader,filewriter,bufferedwriter,bufferedreader, decorative design mode, LineNumberReader