Java Training,Android training,IOS Training,. Net Training , look forward to communicating with you!
"Persistent operation" of the data itself:
-----Data Persistence Technology: All the "intermediate data" (data during processing or processing) is stored in memory while the program is running, but the memory is characterized by the inability to save the data after power-down, so a technique that can store in-memory data for "permanent preservation" is called persistent technology.
"Persist" the data in "file" format:
Method One: Move the in-memory data directly into the disk file------binary file
Method Two: The In-memory data is "logical" to the character, and then written to the file-----text file
"Text file" read and write operations:
Mode one: (Five steps, using FileStream, Streamreader[streamwrite])
1. Create a "file Stream" Object
FileStream fs = new FileStream (FilePath, FileMode.Create);
2. Create a "file Reader" object
StreamWriter SW = new StreamWriter (FS);
3. Read and write operation
Sw. Write (This.textBox1.Text);
4. Close "Reader"
Sw. Close ();
5. Close "File stream"
Fs. Close ();
Method Two: Use the using to simplify the code (reduce the code and steps to close)
1. Create a "file Stream" Object
using (FileStream fs = new FileStream (FilePath, FileMode.Create))
{
2. Create a "file Reader" object
using (StreamWriter SW = new StreamWriter (FS))
{
3. Read and write operation
Sw. Write (This.textBox1.Text);
}
}
Method Three: Direct use of StreamReader, streamwrite
1. Create a "file Reader" object
using (StreamWriter SW = new StreamWriter ("d:\ learner profile. txt"))
{
2. Read and write operation
Sw. Write (This.textBox1.Text);
}
Using "text file" to achieve data persistence, when the data is large and complex, its conversion to "text" process is cumbersome and error-prone, low development efficiency! So we can try to put the "data object" in the entire end to disk file! ------using binary files to save data "serialization technology"
Serialization Operation steps:
1. "Preprocessing" the "pending" Data: Defining the class as serializable "requires all of its families to be serializable"
2. Create a "serialization" Action object:
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
3. Create a "stream" object for the transport:
FileStream fs = new FileStream ();
4. Serialization operation
Create a Stream object (file stream)---responsible for shipping
using (FileStream fs = new FileStream (@ "C:\mydata", FileMode.Open))
{
Create a "serialized object"---Responsible for the object "packaging"
BinaryFormatter bf = new BinaryFormatter ();
Bf. Serialize (FS, _stulist1); Serialization methods
_stulist2 = BF. Deserialize (FS) as list<student>; Deserialization method
}
[Serializable] Serializable identification code
Tips:
Text files read and write, the most prone to error is "garbled":
garbled appearance, because you read and write, the use of "character encoding rules" inconsistent!
Attention:
Read your own files---should adopt consistent "coding rules"
Read a third-party file----should use its corresponding "encoding rules", or directly use the native default encoding rules
"Setup: Set directly to Reader"
StreamReader sr = new StreamReader (Fs,encoding.default);
StreamReader sr = new StreamReader (FS,ENCODING.UTF8)
StreamReader sr = new StreamReader (fs,encoding.getencoding ("gb2312"))
In-depth. NET (file operations)