开发者

writing and reading an arraylist object to and from file

开发者 https://www.devze.com 2022-12-27 10:23 出处:网络
this is simple I know, but i don\'t have internet access and this netcafes keyboard sucks, so if someone can answer this question please.

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

what would be the class ? just 开发者_如何学JAVAgive me a kick in the right direction. there is simple arraylist object that I want to write and read to/ from file. thanks


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


UPDATE:

Here's an example with 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);
        }
    }
}


Since you did not mention what type of data 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 BinaryReader and BinaryWriter classes.

[Edited]

    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();
    }


If your objects in the arraylist are serializable, you can opt for binary serialization. But this means any other application need to know the serialization and then only can use this files. You may like to clarify your intent of using the serialization. So the question remains, why do you need to do a serialization? If it is simple, for you own (this application's) use, you can think of binary serialization. Be sure, your objects are serializable. Otherwise, you need to think of XML serialization.

For Binary serialization, you can think of some code like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号