Java Base serialization deserialization stream implementation serializable interface automatically mounts serial numbers to object text files as modified cannot deserialize object text unless custom long constant print stream

Source: Internet
Author: User
Tags file copy

 PackageCom.swift.baseKnowledge;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.ObjectInputStream;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintStream;ImportJava.io.PrintWriter;/*existing string: "I love Java". Save the string to the A.txt file in the root directory of the current project. Requirement: Save with UTF8 encoding. Procedure Description 1. Create a file byte output stream association destination file 2. Creates a transform output stream object based on the file byte output stream and specifies that the encoding character set is: Uft83. The method that invokes the Stream object writes the string out to the file. 4. Close the stream and release the resource. *//*Use the transform input stream to read the contents of the current project's root directory using the UTF8 encoded a.txt file and print it on the console.    Requirements: Can not be garbled in the case. Procedure Description 1. Create a byte input stream object to specify the file path. 2. Create a transform input stream object based on the byte input stream object and specify the character set encoding: Utf83. Call the Read method of the transform input stream object read content 4. Close Stream Release Resources*//*defines a student class, including name, age, gender, and other member variables, providing setters and getters methods as well as construction methods. Create a student object in the test class that assigns a value to the three member variables of the student object. The object is then serialized into the Stu.txt file in the root directory of the current project. Procedure Description 1. Define the student class and implement the serialization interface and test Class 2. Create a student object in a test Class 3. Create a file byte output stream object and associate the target file 4. Creates an object output stream object from a file byte output stream object 5. Call the object output stream object to save the student object to a file 6. Close the Stream release resource *//*The student object that is serialized in requirement 4 into the Stu.txt file is read. Procedure Description 1. Create a file byte input Stream Object association target file 2. Create an object input stream object based on a file byte input stream object 3. Call the object input stream object to get the student object from the file 4. Close the stream to release the resource. *//*1. Please describe the characteristics of the print flow and the classification of the Print Flow 2. Enter a line of string from the keyboard, and use the byte print stream to save the string to the D.txt file in the current project root directory. Create a byte print stream object and associate the file path 2.2. Call byte Print Stream object The printing method enters the content into the destination file 2.3. Close the Stream release Resource 3. Please briefly outline the configuration classpath add jar package to the project in step 4. To complete the file copy function via Commons-io*/Importjava.io.Serializable; Public classXuliehuaImplementsserializable{ Public Static voidMain (string[] args)throwsIOException, reflectiveoperationexception {outputstreamwriter OSW=NewOutputStreamWriter (NewFileOutputStream ("A.txt"), "Utf-8"); Osw.write ("I love Java");        Osw.flush ();                Osw.close (); InputStreamReader ISR=NewInputStreamReader (NewFileInputStream ("A.txt"), "Utf-8"); Char[] chars=New Char[1024]; StringBuilder SB=NewStringBuilder (); intLen;  while((Len=isr.read (chars))!=-1) {sb.append (chars,0, Len);        } System.out.println (SB); //Student stu=new Student ();//stu.setname ("Zhang San");//Stu.setage (a);//stu.setsex ("male");//ObjectOutputStream oos=new ObjectOutputStream (New FileOutputStream ("Stu.txt"));//Oos.writeobject (stu);//Oos.flush ();//oos.close ();ObjectInputStream ois=NewObjectInputStream (NewFileInputStream ("Stu.txt")); Object obj=Ois.readobject ();                        System.out.println (obj);        System.out.println (System.out); //always true default option, so no need to write and auto brushPrintStream p=NewPrintStream (System.out,true); P.println ("Hello"); P.println ("How is"); PrintStream PS=NewPrintStream (NewFile ("D.txt")); Ps.println ("What should I does for tomorrow.");        Ps.close (); //must write true to automatically brush or not automatically refreshPrintWriter pw=NewPrintWriter (System.out,true); Pw.println ("What"); Pw.println ("Is");                        Pw.close (); //Static cannot be serialized because the serialized content is an object, static is called directly by the class, does not belong to the object sequence, so it is not serialized//do not define static, age does not want to serialize transient transient transient block variables are serialized//Serializable-labeled Interface//after serialization, if you modify the code and no longer serialize, a invalidclassexception sequence number conflict exception occurs//even if the source code is changed, deserialization successfully runs the member property plus static final long seriaversionuid=42l; or the Yellow line on the point class//The print stream applied to Javaweb can be self-refreshed, print as-is char[] Array Direct print string print other array print hash value//Commonsio third-party tools    }}

Java Base serialization deserialization stream implementation serializable interface automatically mounts serial numbers to object text files as modified cannot deserialize object text unless custom long constant print 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.