IO stream 01 -- video Study Notes for bixiangdong JAVA basic tutorial, 01 -- bixiangdong

Source: Internet
Author: User

IO stream 01 -- video Study Notes for bixiangdong JAVA basic tutorial, 01 -- bixiangdong

 

Abstract

01 IO stream (BufferedWriter)
02 IO stream (BufferedReader)
03 IO stream (copying text files through the buffer zone)
04 IO stream (readLine principle)
05 IO stream (MyBufferedReader)
06 IO stream (decorative design mode)
07 IO stream (differences between decoration and inheritance)

01 IO stream (BufferedWriter)
Shard Stream Buffer
The emergence of a buffer increases the efficiency of reading and writing data.
Corresponding class
BufferedWriter
BufferedReader
The buffer must be used in combination with the stream.
The streaming function is enhanced based on the stream.

1/* 2 the emergence of a buffer is to improve the efficiency of stream operations. 3. Before creating a buffer, you must first have a stream object. 4 5 the buffer provides a cross-platform newLine () 6 */7 import java. io. *; 8 public class BufferedWriterDemo 9 {10 public static void main (String [] args) throws IOException11 {12 // create a character writing Stream object 13 FileWriter fw = new FileWriter ("buf.txt"); 14 15 // to improve the character writing efficiency, added buffer 16 // as long as the stream object to be improved as a parameter is passed to the buffer's constructor, 17 BufferedWriter bufw = new BufferedWriter (fw ); 18 19 for (int x = 1; x <5; x ++) 20 {21 bufw. write ("abcde" + x); 22 // write a row separator, which is a cross-platform 23 bufw. newLine (); 24 // remember to refresh 25 bufw as long as the buffer zone is used. flush (); 26} 27 28 // actually, closing the buffer is to close the object 29 bufw in the buffer stream. close (); 30 31 32 33 34} 35}

 


02 IO stream (BufferedReader)

1/* 2 Characters read buffer 3 this buffer provides a method to read a row at a time, readLine () 4 */5 import java. io. *; 6 public class BufferedReaderDemo 7 {8 public static void main (String [] args) throws IOException 9 {10 // create a stream reading object and file-related connection 11 FileReader fr = new FileReader ("buf.txt"); 12 13 // to improve efficiency, add the buffer technology, 14 // pass the character reading Stream object as a parameter to the constructor of the buffer object 15 BufferedReader bufr = new BufferedReader (fr); 16 17 String line; 18 19 while (line = bufr. readLine ())! = Null) 20 {21 System. out. println (line); 22} 23 24 bufr. close (); 25} 26}

 


03 IO stream (copying text files through the buffer zone)

Note: The readLine method only returns the part before the carriage return and does not return the carriage return.

1/* 2 copy one through the buffer zone. java file 3 */4 import java. io. *; 5 public class CopyTextByBuf 6 {7 public static void main (String [] args) 8 {9 BufferedReader bufr = null; 10 BufferedWriter bufw = null; 11 12 try13 {14 bufr = new BufferedReader (new FileReader ("BufferedWriterDemo. java "); 15 bufw = new BufferedWriter (new FileWriter (" buffferWriter_Copy.txt "); 16 17 String line; 18 while (line = bufr. readLine ())! = Null) 19 {20 bufw. write (line); 21 bufw. newLine (); 22 bufw. flush (); 23} 24} 25 catch (IOException e) 26 {27 throw new RuntimeException ("read/write failed! "); 28} 29 finally30 {31 try32 {33 if (bufr! = Null) 34 bufr. close (); 35} 36 catch (IOException e) 37 {38 throw new RuntimeException ("read close failed! "); 39} 40 try41 {42 if (bufw! = Null) 43 bufw. close (); 44} 45 catch (IOException e) 46 {47 throw new RuntimeException ("Write close failed! "); 48} 49} 50} 51}

 


04 IO stream (readLine principle)

Whether it is reading a row or reading multiple characters, they are all read one by one on the hard disk. Therefore, the read method is used for one read at a time.


05 IO stream (MyBufferedReader)

1/* 2 after understanding the principle of reaLine in the BufferedReader class, 3 You can define a class that contains a method with the same functions as readLine. 4. Simulate BufferedReader 5 6 */7 import java. io. *; 8 class MyBufferedReader 9 {10 private FileReader r; 11 MyBufferedReader (FileReader r) 12 {13 this. r = r; 14} 15 // Method for reading a row of data at a time 16 public String myReadLine () throws IOException17 {18 // defines a temporary container, the original BufferReader encapsulates the character array 19 // for demonstration convenience, define a StringBuilder container 20 // because the data is still to be converted into a string 21 22 StringBuilder sb = new StringBuilder (); 23 int ch = 0; 24 while (ch = r. read ())! =-1) 25 {26 if (ch = '\ R') 27 continue; 28 if (ch =' \ n') 29 return sb. toString (); 30 else31 sb. append (char) ch); 32} 33 if (sb. length ()! = 0) 34 return sb. toString (); 35 return null; 36} 37 public void myClose () throws IOException 38 {39 r. close (); 40} 41} 42 43 public class MyBufferedReaderDemo44 {45 public static void main (String [] args) throws IOException46 {47 FileReader fr = new FileReader ("buf.txt "); 48 49 MyBufferedReader myBuf = new MyBufferedReader (fr); 50 String line = null; 51 while (line = myBuf. myReadLine ())! = Null) 52 {53 System. out. println (line); 54} 55 myBuf. myClose (); 56 57} 58}

 


06 IO stream (decorative design mode)

1/* 2 Decoration Design Mode: 3. When you want to enhance the functions of an existing object, 4 define a class and pass in existing objects. Based on the existing functions, and provide enhanced functions 5 so that the custom class will become the decorative Class 6 7 the decorative class will usually receive the decorated object through the constructor 8 and based on the function of the decorated object, provides enhanced features 9 */10 class Person11 {12 public void chiFan () 13 {14 System. out. println ("dinner"); 15} 16} 17 class SuperPerson18 {19 private Person p; 20 public SuperPerson (Person p) 21 {22 this. p = p; 23} 24 public void superChiFan () 25 {26 System. out. println ("appealer"); 27 p. chiFan (); 28 System. out. println ("dessert"); 29 System. out. println (""); 30 31} 32} 33 public class PersonDemo 34 {35 public static void main (String [] args) 36 {37 SuperPerson sp = new SuperPerson (new Person (); 38 sp. superChiFan (); 39 40} 41}

 


07 IO stream (differences between decoration and inheritance)

Use the inheritance system to enhance Functions
MyReader // class specifically used to read data
| -- MyTextReader
| -- MyBufferTextReader
| -- MyMediaTextReader
| -- MyBufferMediaReader
| -- MyDataReader
| -- MyBufferDataReader

Class MyBufferReader
{
MyBufferReader (MyTextReader text)
{}
MyBufferReader (MyMediaTextReader media text)
{}

}
The above class has poor scalability.
Find the common type of its parameters and improve scalability through Polymorphism
Class MyBufferReader extends MyReader
{
MyBufferReader (MyReader r)
{}
}


Use decoration to enhance Functions
MyReader // class specifically used to read data
| -- MyTextReader
| -- MyMediaReader
| -- MyDataReader
| -- MyBufferReader

The decoration mode is more flexible than inheritance, avoiding the bloated inheritance system.
It also reduces the relationship between classes.

Because the decoration class enhances existing objects, it has the same functions as existing objects,
It only provides stronger functions.
Therefore, the decoration class and the decoration class usually belong to a system.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.