Java files and io--print streams and object flows

Source: Internet
Author: User

The main function of the print stream is for the output, and the print stream is divided into two types in the entire IO package:

BYTE print stream: PrintStream

Character Print stream: PrintWriter

The print flow can be easily output

 Public classPrintstreamdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method Stubbytestreamout ();    Charstreamout (); }    //PrintStream     Public Static voidbytestreamout () {Try{OutputStream out=NewFileOutputStream ("G:\\hhb.txt"); Bufferedoutputstream Bos=NewBufferedoutputstream (out); PrintStream PS=NewPrintStream (BOS);//more convenient output//bos.write ("Hhhhh". GetBytes ());PS.PRINTLN (123);            Ps.close ();            Bos.close ();        Out.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }    //PrintWriter     Public Static voidcharstreamout () {Try{Writer W=NewFileWriter ("G:\\hhh.txt"); BufferedWriter BW=NewBufferedWriter (W); PrintWriter PW=Newprintwriter (BW); Pw.println (343); Pw.println ("LLL"); //bw.write ("I love you!" ");Pw.close ();            Bw.close ();        W.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Object Flow

Serialization of objects

Object serialization refers to the conversion of an object into a binary byte stream.

Two cases:

The operation of saving an object on a file is called a serialization operation of an object

Recovering an object from a file is called a deserialization operation

Use the ObjectOutputStream class to implement serialization of objects, using the ObjectInputStream class to implement deserialization.

The class where the serialized object resides must implement the Java.io.Serializable interface

 Public classCatImplementsserializable{PrivateString name; Private intAge ; Private transientString color;//keyword transient, which means no serialization     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicCat (String name,intAge ) {        Super();  This. Name =name;  This. Age =Age ; } @Override PublicString toString () {return"Cat [name=" + name + ", age=" + Age + "]"; }     PublicCat () {Super(); //TODO auto-generated Constructor stub    }    }
 Public classObjectstreamdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method Stubobjectout ();    Objectin (); }    //implementing serialization of Objects     Public Static voidObjectout () {Cat cat=NewCat ("Mimi", 3); Try{OutputStream out=NewFileOutputStream ("G://555.kds"); ObjectOutputStream Oos=NewObjectOutputStream (out);            Oos.writeobject (CAT);            Oos.close ();        Out.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    //Implementing deserialization     Public Static voidObjectin () {Try{InputStream in=NewFileInputStream ("G://555.kds"); ObjectInputStream Ois=NewObjectInputStream (in); Cat Cat=(Cat) ois.readobject ();            System.out.println (CAT);            Ois.close ();        In.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Java files and io--print streams and object flows

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.