Java:io System and Decoration mode

Source: Internet
Author: User
Tags alphanumeric characters

There are three types of IO streams in Java:

① input stream, output stream: The input and output stream is referenced by a Java program.

② byte stream: bytes are storage units, accounting for 8 bits, and other basic data types are measured in bytes. Characters are alphanumeric characters, ASCII and Unicode are character encoding sets, ASCII codes are 8 bits in one byte, Unicode is 16 bits and two bytes, and Java character encoding is Unicode. The byte stream suffix is stream, and the character stream suffix is reader,writer.

③ node flow, processing flow: The node flow can be understood as the real flow of data processing, processing flow is on the basis of node flow modification.

For the classification of various streams, refer to the following articles: http://blog.csdn.net/yuebinghaoyuan/article/details/7388059


Example one: FileInputStream, FileOutputStream, and character node streams with byte node streams filereader, FileWriter read and write files

Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.filewriter;import Java.io.ioexception;public class Test {public static void main ( String[] args) {FileInputStream FIS = null; FileOutputStream fos = null; FileReader FR = null;  FileWriter fw = null;try {fis = new FileInputStream ("D:/from.txt");//Create a new From.txt text file on D-disk with ABCD byte[] buffer = new BYTE[64]; Reads data from the input stream into the byte array int temp = fis.read (buffer); System.out.println (Buffer[0]); Unicode encoding, so the ASCII code 97system.out.println (new String (buffer)) is printed, and FOS = new FileOutputStream ("D://to.txt"); Writes the data of the byte array to the To.txt text file fos.write (buffer, 0, temp); Read and write large files with a while loop can be FR = new FileReader ("D:/from1.txt"); Ibid. fw = new FileWriter ("D:/to1.txt"); Content is zxcvchar[] buf = new Char[64];int Temp1 = Fr.read (BUF); System.out.println (Buf[0]); System.out.println (New String (BUF)); Fw.write (buf, 0, temp1);} catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();} finally {//will all input Stream and output stream close try {fis.close ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();} try {fos.close ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();} try {fr.close ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();} try {fw.close ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}}}
Printing results:

97abcdzzxcv


Example two: Withmodifier character Node streamFileReader'sBufferedReader processing streams to read and write files

Import Java.io.bufferedreader;import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;public class Test {public static void main (string[] args) {//TODO auto-generated method stub filereader fr = null; BufferedReader br = null;try {fr = new FileReader ("D:/from2.txt");//content is the first line: ABCD, second line: EFGH, third line: IJKBR = new BufferedReader ( FR); The passed argument is a node character stream string Line;while (line = Br.readline ()) = null) {//This processing stream has a ReadLine method than the node stream, One-time System.out.println (line) can be read at once;}} catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch block E. Printstacktrace ();} Finally {try {fr.close ()} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();} try {br.close ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}}}
Printing results:

Abcdefghijk

The process flow is to add some functionality on the basis of the node stream, which is a decorative pattern.


Example three: simple decorative pattern understanding

① creating a new student interface

Public interface Student {public void study ();}
② new Two implementation classes

public class Wenkestudent implements Student {private String name;public wenkestudent (String name) {this.name = name;} @Overridepublic Void Study () {//TODO auto-generated method stub System.out.println (name+ "Learning Arts");}

public class Likestudent implements Student {private String name;public likestudent (String name) {this.name = name;} @Overridepublic Void Study () {//TODO auto-generated method stub System.out.println (name + "Learning Science");}}

③ Create a new decoration class

public class Decoratestudent {private Student Student;   This is the key to the whole pattern, in the adornment class there is a decorator's interface reference <span style= "White-space:pre" ></span>   // The arguments that are passed in the constructor should be the implementation class object of the interface to be transformed upward into an interface <span style= "White-space:pre" ></span>public decoratestudent (Student Student) {super (); this.student = student;} public void Study_well () {  //Add two modifiers student.study (); System.out.println ("very strong");} public void Study_bad () {student.study (); System.out.println ("very poor");}}

④main function

public class Test {public static void main (string[] args) {//TODO auto-generated method stub decoratestudent decoratestudent = new decorate Student (New Likestudent ("Zhang San"));d Ecoratestudent.study_well ();D ecoratestudent decorateStudent2 = new Decoratestudent ( New Wenkestudent ("John Doe"));d Ecoratestudent2.study_bad ();}}
Printing results:

Zhang San study science very strong John Doe study liberal arts very poor

and IO system as a reference to analyze the above example: Student interface equivalent to InputStream, OutputStream, Reader, writer and other base classes; Wenkestudent, Likestudent equivalent to FileInputStream, FileOutputStream, FileReader, FileWriter and other inheriting classes The Decoratestudent class is equivalent to the BufferedReader class, and the Study_well method is equivalent to the ReadLine method. So the contrast should be clear.






Java:io System and Decoration mode

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.