tags:

views:

83

answers:

3

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 give me a kick in the right direction. there is simple arraylist object that I want to write and read to/ from file. thanks

+2  A: 

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);
        }
    }
}
Darin Dimitrov
i don't care bout the format of the file. the arraylist object holds string objects. now please give me atleast one definitive answer
I think he also gave a very good answer. You should probably research BinaryFormatter class now.
Nayan
@user257412, please see my update.
Darin Dimitrov
+1  A: 

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();
    }
Nayan
i am not sure , but from what i remember binarywriter's write method doesn't write collections object, only primitive types. so its prolly of no use
Really? Have you used those classes before? See my edited answer.
Nayan
A: 

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();
Kangkan
i got several acceptable answers, thanks all