Java c-sharp USES binary serialization deserialization operation data
- 2020-04-01 03:31:37
- OfStack
Java USES binary serialization, deserialization operations, first, to introduce the following java.io related packages, or directly write import java.io.*;
Next, for ease of writing, copy files, and throws declare exceptions
public void test6() throws IOException {
byte[] b = new byte[1024];//Define byte array, buffer
FileInputStream in = new FileInputStream("E:\logo.gif");//Create an input stream object
FileOutputStream out = new FileOutputStream("E:\My.gif");//Create an output stream object
DataInputStream input = new DataInputStream(in);//Create the input binary stream
DataOutputStream dout = new DataOutputStream(out);//Create the output binary stream
int num = input.read(b);//Read the binary file into b
while (num != -1) {
dout.write(b, 0, num);//Writes the array read to the output stream
num = input.read(b);//Read
again
}
//Close all stream objects
in order
input.close();
dout.close();
in.close();
out.close();
System.out.println(" Copy successful! ");
}
Preliminary code, for reference only!
C # using Binary Serialization and deserialization operation first, introducing the namespace using System. The Runtime. Serialization. Formatters. Binary; To manipulate serialization and deserialization
Also, add an indicator class [Serializable] above the class that involves the serialized custom class
Example:
[Serializable]
public class PlayManager
{
/// <summary>
/// serialized save data
/// </summary>
public void Save()
{
FileStream fs = null;
try
{
fs = new FileStream(" Save the path to the file ", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, The object to save );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
fs.Close();
}
/// <summary>
/// load serialization information
/// </summary>
public void Load()
{
FileStream fs = null;
try
{
fs = new FileStream(" The file path ", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
Object to receive = ( Type of object )bf.Deserialize(fs); //Casts
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
fs.Close();
}
That's how you serialize files in C#, and it's actually pretty simple. If you don't add try-catch-finally, it's just four lines of code.
Past friends, do you see? Do not understand still can ask!