One, binary reading and writing classes:
1. BinaryReader/BinaryWriter: Binary Reading and Write
- BinaryReader: Read the primitive data type as a binary value with a specific encoding.
- BinaryWriter: Write primitive types to streams in binary form and supports writing strings with specific encodings.
2. XmlReader/XmlWriter: XML reading and writing
See:C# Use XmlReader and XmlWriter to operate XML files
2. BinaryReader/BinaryWriter
The primitive data type of the read and write stream. It can operate binary files such as images and compressed files. It can also be MemoryStream, etc.
There is no need to operate byte byte, it can be operated like 2, 4, or 8 bytes.
A character or number can be written in a specified number of bytes.
1. Write:
using (BinaryWriter writer = new BinaryWriter((fileName, ))) { (1.250F); (@"c:\Temp"); (10); (true); }
() method outputs binary image
FileStream fs = new FileStream(("Unnamed.jpg"), );//Save the image file in the file streamlong fslength = ;//Flow lengthbyte[] b=new byte[(int)fslength];//Define binary array(b, 0, (int)fslength);//Write bytes in the stream into a binary array();//Close the flow = "image/jpg";//Without this, there will be garbled code(b);//Output the image on the page
2. Read:
Each read returns to the corresponding number of bytes in the current position in the lift stream.
The following code example demonstrates how to store and retrieve application settings in a file.
const string fileName = ""; float aspectRatio; string tempDirectory; int autoSaveTime; bool showStatusBar; if ((fileName)) { using (BinaryReader reader = new BinaryReader((fileName, ))) { aspectRatio = (); tempDirectory = (); autoSaveTime = reader.ReadInt32(); showStatusBar = (); } ("Aspect ratio set to: " + aspectRatio); ("Temp directory is: " + tempDirectory); ("Auto save time set to: " + autoSaveTime); ("Show status bar: " + showStatusBar); }
BinaryReader reads pictures:
using (FileStream fs = new FileStream("", , )) { //Save the image as a file stream using (BinaryReader br = new BinaryReader(fs)) { byte[] imgBytesIn = ((int)); //Read the stream into the byte array (); } }
3. Serialize the object BinaryFormatter in binary format
1. SoapFormatter (used in HTTP) and BinaryFormatter (used in TCP) classes implement the IFormatter interface (inherited from IRemotingFormatter, supporting remote procedure calls (Rpc))
- Deserialize(Stream) Deserializes the data in the provided stream and recompiles the object graph.
- Serialize(Stream, Object) Serializes an object or object with a given root to the provided stream.
See:https:///article/
2. Give an example:
[Serializable] public class Product //Entity Class{ public long Id; [NonSerialized]//Identity does not serialize this member Name public string Name; public Product(long Id, string Name) { = Id; = Name; } } static void Main() { //Serialization (object saved to file) List<Product> Products = new List<Product> { new Product(1,"a"),new Product(2,"b") }; FileStream fs = new FileStream("", ); IFormatter formatter = new BinaryFormatter(); (fs, Products); (); //Deserialization (convert file content into object) FileStream fs1 = new FileStream("", ); BinaryFormatter formatter1 = new BinaryFormatter(); List<Product> addresses = (List<Product>)(fs1); (); foreach (Product de in addresses) { ("{0} lives at {1}.", , ); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.