vb.net 将数组保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9427099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:57:45  来源:igfitidea点击:

Save array to file

vb.net

提问by Garry

I have a string array that is populated with thousands of entries. I am wanting the quickest way to save this data to disk and to also load it back up.

我有一个填充了数千个条目的字符串数组。我想要以最快的方式将此数据保存到磁盘并将其加载回来。

Currently I am looping through the array and appending this data to a string and then saving this string. This takes a long time.

目前我正在遍历数组并将此数据附加到一个字符串,然后保存此字符串。这需要很长时间。

What is the fastest/most efficient way to do this?

最快/最有效的方法是什么?

Thanks

谢谢

回答by Stefan

Write

 Dim FileName as string=Application.StartupPath & "\myarray.txt"
 IO.File.WriteAllLines(FileName,myarray)

Read

 Dim FileName as string=Application.StartupPath & "\myarray.txt"
 Dim myarray() As String = File.ReadAllLines(FileName)

回答by Avner Shahar-Kashtan

How important is it that the resulting file be human-readable? If it's mostly there to be read later by the program, you should definitely use serialization:

结果文件的可读性有多重要?如果程序稍后读取它,则绝对应该使用序列化:

public void SaveArray (string[] array)
{
    NetDataContractSerializer serializer = new NetDataContractSerializer();
    using (FileStream file = System.IO.File.Create(_filename))
    {
         serializer.Serialize(file, array);
    }
}

Edit:

编辑:

To summarize the advantages of this method over the other two suggested below, the first being using a StringBuilder to concatenate a big string and then saving it to disk, and the second using WriteAllLines to write the string array to disk:

总结此方法相对于下面建议的其他两个方法的优点,第一个是使用 StringBuilder 连接一个大字符串,然后将其保存到磁盘,第二个使用 WriteAllLines 将字符串数组写入磁盘:

The first method will completely lose fidelity to the original list of strings, since it will glob them together into one string. Can you separate them later? Are there well-known delimiters between them? Not necessarily. It's more efficient than just calling + between them, but it's not a good way to store them.

第一种方法将完全失去对原始字符串列表的保真度,因为它将把它们组合成一个字符串。以后可以分开吗?它们之间是否有众所周知的分隔符?不必要。这比在它们之间调用 + 更有效,但这不是存储它们的好方法。

The second method is better, since each string will be saved to a different line, and can be read back with ReadAllLines, but you're still on shaky ground here. What happens if any string contains the NewLine character? It will be one string when written, but two different strings when read back. Again, calling WriteAllLines/ReadAllLines relies on newlines being good delimiters, when that's not necessarily the case.

第二种方法更好,因为每个字符串都将保存到不同的行,并且可以使用 ReadAllLines 读回,但您仍然处于不稳定状态。如果任何字符串包含 NewLine 字符会发生什么?写入时将是一个字符串,但回读时将是两个不同的字符串。同样,调用 WriteAllLines/ReadAllLines 依赖于作为良好分隔符的换行符,但事实并非如此。

Using a serializer - and it doesn't matter if it's the NetDataContractSerializer, BinaryFormatter or any other - will maintain full fidelity with your original data structure, with only a little bit of overhead.

使用序列化程序 - 无论是 NetDataContractSerializer、BinaryFormatter 还是其他任何类型都无关紧要 - 将保持与原始数据结构的完全保真度,只需要一点点开销。

回答by samirahmed

Try using StringBuilder

尝试使用 StringBuilder

See the msdn referencefor more

有关更多信息,请参阅msdn 参考

Why stringbuilder?

为什么是字符串生成器?

When dealing with large files. It is advisible to use the StringBuilder class rather than the regular string class. Appending strings together, requires the old string to be copied into a new string. String Builders are buffers that can change size easily and you can append and delete faster.

处理大文件时。建议使用 StringBuilder 类而不是常规字符串类。将字符串附加在一起,需要将旧字符串复制到新字符串中。字符串构建器是可以轻松更改大小的缓冲区,您可以更快地追加和删除。

回答by Ali Issa

You should loop through your data using a string builder which is much faster than ordinary string.

您应该使用比普通字符串快得多的字符串构建器来遍历数据。

then create a stream writer/ reader (when you want to load the file) from IO library and create the file on your disk.

然后从 IO 库创建一个流写入器/读取器(当你想加载文件时)并在你的磁盘上创建文件。