JAVA print stream and transform stream

Source: Internet
Author: User

The conversion flow is mainly two inputstreamreader and OutputStreamWriter

1. InputStreamReader is mainly to convert the input stream of the byte stream into the character input flow

2. OutputStreamWriter is mainly to convert the output stream of the byte stream into a character output flow

The print stream consists of two main: PrintStream and PrintWriter, respectively, corresponding to byte stream and character stream.

Completing the redirect for screen printing

System.out corresponds to the PrintStream, the default in the output in the console, we can redirect his output, can be directed to the file

That is, the execution of System.out.println ("Arry"); Output to a file without outputting to the screen

/*system.in can receive input from the screen*/ImportJava.io.*; Public classprintstreamtest02{ Public Static voidMain (string[] args) {bufferedreader br=NULL; Try{InputStreamReader ISR=NewInputStreamReader (system.in); BR=NewBufferedReader (ISR); String Temp=NULL;  while(temp = Br.readline ())! =NULL) {System.out.println (temp); //if output w exits the loop                if("W". Equals (temp)) {                     Break; }                }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(BR! =NULL) {br.close (); }            }Catch(IOException e) {e.printstacktrace (); }        }    }}
/*To complete the screen printing redirect System.out corresponds to the PrintStream, the default in the output in the console, we can redirect his output, can be directed to the file is executed System.out.println ("Arry"); Output to a file without outputting to the screen*/ImportJava.io.*; Public classprintstreamtest01{ Public Static voidMain (string[] args) {FileOutputStream fos=NULL; Try{fos=NewFileOutputStream ("C:\\work\\java\\arry.txt"); PrintStream PS=NewPrintStream (FOS);                        System.setout (PS); System.out.println ("Arry teacher is so handsome!" Is handsome! "); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(Fos! =NULL) {fos.close (); }            }Catch(IOException e) {e.printstacktrace (); }        }    }}
/*To complete the screen printing redirect System.out corresponds to the PrintStream, the default in the output in the console, we can redirect his output, can be directed to the file is executed System.out.println ("Arry"); Output to a file without outputting to the screen*/ImportJava.io.*; Public classprintstreamtest01{ Public Static voidMain (string[] args) {FileOutputStream fos=NULL; Try{fos=NewFileOutputStream ("C:\\work\\java\\arry.txt"); PrintStream PS=NewPrintStream (FOS);                        System.setout (PS); System.out.println ("Arry teacher is so handsome!" Is handsome! "); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(Fos! =NULL) {fos.close (); }            }Catch(IOException e) {e.printstacktrace (); }        }    }}

Object flow can convert a Java object to a binary write disk, a process that is often called serialization and can

To read the complete Java object from disk, and this process is called deserialization.

Object flow mainly includes: ObjectInputStream and ObjectOutputStream

How to implement serialization and deserialization

/*OutputStreamWriter is mainly to convert the output stream of a byte stream into a character output flow*/ImportJava.io.*; Public classoutputstreamwritertest01{ Public Static voidMain (string[] args) {BufferedWriter bw=NULL; Try{                        //byte output stream//FileOutputStream fos = new FileOutputStream ("C:\\work\\java\\arry.txt"); //character output stream//OutputStreamWriter OSW = new OutputStreamWriter (FOS);BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream ("C:\\work\\java\\arry.txt"))); Bw.write ("Keke teacher married happy!" ");                        Bw.newline (); Bw.write ("Blessed Master and Mrs. Song Newlyweds Happy, Eternal love, grow old, early born expensive son!!!!!");                Bw.flush (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{            Try{                if(BW! =NULL) {bw.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }    }}
/*Object Flow: An object stream can convert a Java object to a binary write disk, a process called serialization that can also read a complete Java object from disk, which is called deserialization: objecti Nputstream and ObjectOutputStream java.io.Serializable*/ImportJava.io.*; Public classobjectstreamtest02{ Public Static voidMain (string[] AGS) {ObjectInputStream ois=NULL; Try{FileInputStream fis=NewFileInputStream ("C:\\work\\java\\arry.txt"); Ois=NewObjectInputStream (FIS); //deserializationStudent stu =(Student) ois.readobject ();                                System.out.println (Stu.name); }Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); }Catch(ClassNotFoundException e) {e.printstacktrace (); } finally{            Try{                if(Ois! =NULL) {ois.close (); }            }Catch(IOException e) {e.printstacktrace (); }        }            }}//Implementing a serialized interfaceclassStudentImplementsserializable{String name;}

If you implement serialization, the class must implement the serialization interface java.io. Serializable, the interface does not have any method, the interface

is just a markup interface that marks the class that can be serialized

JAVA print stream and transform stream

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.