序列化嵌套列表或替代项
Serialise nested list or alternatives
我正在尝试存储一组列表(每个列表包含超过 20.000 个 int),并且希望为此使用嵌套的 lest,因为每天都会添加一个新列表。
最终我需要通过以下方式访问数据:
"Take the first value of each list and compile a new list"。
理想情况下,我想序列化一个 List<List<int>>
,但这似乎不起作用(我可以序列化一个 List<int>
)。这样做有技巧吗(最好不要使用任何插件)?
如果没有,您会如何建议我高效快速地存储此类数据?
我现在尝试的方式:
static void saveFunction(List<int> data, string name)
{
using (Stream stream = File.Open(name + ".bin", FileMode.OpenOrCreate))
{
BinaryFormatter bin = new BinaryFormatter();
if (stream.Length == 0)
{
List<List<int>> List = new List<List<int>>();
List.Add(data);
bin.Serialize(stream, List);
}
else
{
List<List<int>> List = (List<List<int>>)bin.Deserialize(stream);
List.Add(data);
bin.Serialize(stream, List);
}
}
}
奇怪的是 list.Count 仍然是 1,并且列表中的 int 数也保持不变,同时文件大小增加了。
您需要倒流并在读写之间清除之前的数据:
static void saveFunction(List<int> data, string name)
{
using (Stream stream = File.Open(name + ".bin", FileMode.OpenOrCreate))
{
BinaryFormatter bin = new BinaryFormatter();
if (stream.Length == 0)
{
var List = new List<List<int>>();
List.Add(data);
bin.Serialize(stream, List);
}
else
{
var List = (List<List<int>>)bin.Deserialize(stream);
List.Add(data);
stream.SetLength(0); // Clear the old data from the file
bin.Serialize(stream, List);
}
}
}
您现在正在做的是将新列表附加到文件末尾,同时保留旧列表原样 -- BinaryFormatter
将很高兴地读取为文件中的(第一个)对象它被重新打开。
关于您的第二个问题,"how would you advice me to store such data efficiently and quick?",由于您的计划是 "take the first value of each list and compile a new list",看来您在编写新列表时需要重新阅读前面的列表。但是,如果那不是真的,并且每个新列表都独立于前面的列表,那么 BinaryFormatter
确实支持将多个根对象写入同一文件。详情请看这里:Serializing lots of different objects into a single file
我正在尝试存储一组列表(每个列表包含超过 20.000 个 int),并且希望为此使用嵌套的 lest,因为每天都会添加一个新列表。
最终我需要通过以下方式访问数据:
"Take the first value of each list and compile a new list"。
理想情况下,我想序列化一个 List<List<int>>
,但这似乎不起作用(我可以序列化一个 List<int>
)。这样做有技巧吗(最好不要使用任何插件)?
如果没有,您会如何建议我高效快速地存储此类数据?
我现在尝试的方式:
static void saveFunction(List<int> data, string name)
{
using (Stream stream = File.Open(name + ".bin", FileMode.OpenOrCreate))
{
BinaryFormatter bin = new BinaryFormatter();
if (stream.Length == 0)
{
List<List<int>> List = new List<List<int>>();
List.Add(data);
bin.Serialize(stream, List);
}
else
{
List<List<int>> List = (List<List<int>>)bin.Deserialize(stream);
List.Add(data);
bin.Serialize(stream, List);
}
}
}
奇怪的是 list.Count 仍然是 1,并且列表中的 int 数也保持不变,同时文件大小增加了。
您需要倒流并在读写之间清除之前的数据:
static void saveFunction(List<int> data, string name)
{
using (Stream stream = File.Open(name + ".bin", FileMode.OpenOrCreate))
{
BinaryFormatter bin = new BinaryFormatter();
if (stream.Length == 0)
{
var List = new List<List<int>>();
List.Add(data);
bin.Serialize(stream, List);
}
else
{
var List = (List<List<int>>)bin.Deserialize(stream);
List.Add(data);
stream.SetLength(0); // Clear the old data from the file
bin.Serialize(stream, List);
}
}
}
您现在正在做的是将新列表附加到文件末尾,同时保留旧列表原样 -- BinaryFormatter
将很高兴地读取为文件中的(第一个)对象它被重新打开。
关于您的第二个问题,"how would you advice me to store such data efficiently and quick?",由于您的计划是 "take the first value of each list and compile a new list",看来您在编写新列表时需要重新阅读前面的列表。但是,如果那不是真的,并且每个新列表都独立于前面的列表,那么 BinaryFormatter
确实支持将多个根对象写入同一文件。详情请看这里:Serializing lots of different objects into a single file