Writing and reading an arraylist object to and from a file

It's simple, I know, but I don’t have internet access, and this netcafes keyboard sucks, so if someone can answer this question, please.

what will be the class? just give me a punch in the right direction. there is a simple arraylist object that I want to write and read to / from a file. thank

+3
source share
3 answers

There is no single answer to this question. This will depend on the file format and the objects in the list. You need a serializer. For example, you can use BinaryFormatter , which serializes an instance of an object into a binary file, but your objects must be serializable . Another option is the XmlSerializer , which uses the XML format.


UPDATE:

Here is an example with a BinaryFormatter:

class Program
{
    static void Main()
    {
        var list = new ArrayList();
        list.Add("item1");
        list.Add("item2");

        // Serialize the list to a file
        var serializer = new BinaryFormatter();
        using (var stream = File.OpenWrite("test.dat"))
        {
            serializer.Serialize(stream, list);
        }

        // Deserialize the list from a file
        using (var stream = File.OpenRead("test.dat"))
        {
            list = (ArrayList)serializer.Deserialize(stream);
        }
    }
}
+4
source

Since you did not specify what data type this array contains, I would suggest writing the file in binary format.

Here is a good tutorial on how to read and write in binary format.

Basically, you need to use the BinaryReaderand classes BinaryWriter.

[Changed]

    private static void write()
    {
        List<string> list = new List<string>();
        list.Add("ab");
        list.Add("db");
        Stream stream = new FileStream("D:\\Bar.dat", FileMode.Create);
        BinaryWriter binWriter = new BinaryWriter(stream);
        binWriter.Write(list.Count);
        foreach (string _string in list)
        {
            binWriter.Write(_string);
        }
        binWriter.Close();
        stream.Close();
    }

    private static void read()
    {
        List<string> list = new List<string>();
        Stream stream = new FileStream("D:\\Bar.dat", FileMode.Open);
        BinaryReader binReader = new BinaryReader(stream);

        int pos = 0;
        int length = binReader.ReadInt32();
        while (pos < length)
        {
            list.Add(binReader.ReadString());
            pos ++;
        }
        binReader.Close();
        stream.Close();
    }
+1
source

If your objects in the arraylist are serializable, you can choose binary serialization. But this means that any other application needs to know serialization, and then only use these files. You can clarify the intention to use serialization. So the question remains: why do you need to serialize? If it's simple, for you (this app), you might consider binary serialization. Make sure your objects are serializable. Otherwise, you need to think about XML serialization.

For binary serialization, you can come up with the following code:

Stream stream = File.Open("C:\\mySerializedData.Net", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, myArray);
stream.Close();
0
source

Source: https://habr.com/ru/post/1741562/


All Articles