在文件中写入和读取 arraylist 对象

发布于 2024-08-29 17:35:41 字数 121 浏览 1 评论 0原文

我知道这很简单,但我没有互联网接入,而且这个网吧键盘很糟糕,所以如果有人可以回答这个问题。

课程是什么?只要朝正确的方向踢我一脚即可。有一个简单的 arraylist 对象,我想将其写入文件或从文件中读取。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉徒眸意 2024-09-05 17:35:41

这个问题没有一个明确的答案。这取决于文件的格式和列表中的对象。你需要一个序列化器。例如,您可以使用 BinaryFormatter 将对象实例序列化为二进制文件,但您的对象必须是 可序列化。另一种选择是使用 XML 的 XmlSerializer格式。


更新:

这是 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);
        }
    }
}

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);
        }
    }
}
相对绾红妆 2024-09-05 17:35:41

由于您没有提到该数组包含什么类型的数据,我建议以二进制格式编写该文件。

这是一个关于如何读写的很好的教程二进制格式。

基本上,您需要使用 BinaryReaderBinaryWriter 类。

[编辑]

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

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();
    }
写下不归期 2024-09-05 17:35:41

如果数组列表中的对象是可序列化的,则可以选择二进制序列化。但这意味着任何其他应用程序都需要知道序列化,然后才能使用该文件。您可能想澄清您使用序列化的意图。那么问题来了,为什么需要进行序列化呢?如果很简单,供您自己(此应用程序)使用,您可以考虑二进制序列化。确保您的对象是可序列化的。否则,您需要考虑 XML 序列化。

对于二进制序列化,您可以想到这样的代码:

Stream stream = File.Open("C:\\mySerializedData.Net", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, myArray);
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文