Record: file read/write

Source: Internet
Author: User

Objects are the objects currently concerned by the subject. Classes and structures are actually templates for creating objects. Each object contains data and provides methods to process and access data.

Of course, we all hope that the storage of things will take a long time. When we design programs and run programs, when we run programs, the objects will be gone, so how can we store it for a long time? Persistence is a way to store objects in the memory to storage devices such as disks, files, or streams that can be permanently stored. Objects in the memory must be persistently stored. There are three methods for reading and writing files: file stream reading and writing, XML file reading and writing, serialization and deserialization. These three methods have their own merits ......

File stream reading and writing are generally implemented using ReadLine () in StreamReader and WriteLine () in SteamWriter;

XML documents are generally loaded using the Load () method in the document, but also through some of the classes: XmlElenment, XmlAttribute, XmlNode, and other methods;

Persistence mainly refers to serialization and deserialization:

Serialization Process
1. Set the object class to serializable.
2. Create an object class object.
3. Create a serialization Tool Object.
4. File streams should be written)
5. serialization.
6. Clear resources during serialization.
Deserialization Process
1. Create a serialization Tool Object.
2. Create a file stream and read it)
3. deserialization.
4. Clear resources during serialization.

BinaryFormatter: Content serialized in binary serialization mode. It can only be read and written on the. net platform.
Xml serialization can share data among multiple technical platforms.
The Xml serialization method requires that the class must be a public class.

Here is a simple example:

 

Namespace prjIOtest6 {[Serializable] // set the object class to Serializable class Card: IComparable <Card> {string name; public string Name {

* ***: Step 1: This is required. serialization and deserialization can be started only after it is set.

 

Card card = new Card (); // create an object card. name = "dongjing"; card. phone = "15191143820"; card. qq = "1234564556"; card. e-mail = "yddongjingjing@163.com"; card. address = "Shaanxi Xianyang wugong"; BinaryFormatter bf = new BinaryFormatter (); // create the serialization Tool Object FileStream fs = new FileStream ("test. dat ", FileMode. create); // Create a stream to write to bf. serialize (fs, card); // Serialize fs. flush (); fs. close ();

Step 2: serialization Process

 

BinaryFormatter bf = new BinaryFormatter (); FileStream fs = new FileStream ("test. dat ", FileMode. open); // create a stream to read object obj = bf. deserialize (fs); // Deserialize Card card = (Card) obj; // forcibly convert the object type to the Card type Console. writeLine (card); fs. close ();

Step 3: deserialization

The above is the entire process of persistence.

P.S: as is used to convert data types. as objects are used before conversion, and reference types are used later)
Is used to determine whether an object is of a certain type
Typeof is used to obtain the Type object of a class of an object.

Bit OPERATOR: & | ^ ~ >>< <

 

 

This article from the "Lanting drunk beauty" blog, please be sure to keep this source http://7607889.blog.51cto.com/7597889/1293747

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.