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