将大字符串写入文件的最佳方法

发布于 2024-10-18 03:12:04 字数 673 浏览 3 评论 0原文

在 C# 中,我正在读取中等大小的文件(100 KB ~ 1 MB),修改部分内容,最后写入不同的文件。所有内容均为文字。修改是作为字符串对象和字符串操作完成的。我当前的方法是:

  1. 使用 StreamReader 读取原始文件中的每一行。
  2. 打开一个 StringBuilder 来存放新文件的内容。
  3. 修改字符串对象并调用StringBuilderAppendLine(直到文件末尾)
  4. 打开一个新的StreamWriter,并写入StringBuilder 到写入流。

但是,我发现 StremWriter.Write 截断了 32768 字节 (2^16),但 StringBuilder 的长度大于此值。我可以编写一个简单的循环来保证将整个字符串写入文件。但是,我想知道 C# 中完成此任务最有效的方法是什么?

总而言之,我只想修改文本文件的某些部分并写入不同的文件。但是,文本文件大小可能大于 32768 字节。

== 回答 == 很抱歉给您带来困扰!只是我没有调用flushStremWriter.Write 没有短(例如,2^16)限制。

In C#, I'm reading a moderate size of file (100 KB ~ 1 MB), modifying some parts of the content, and finally writing to a different file. All contents are text. Modification is done as string objects and string operations. My current approach is:

  1. Read each line from the original file by using StreamReader.
  2. Open a StringBuilder for the contents of the new file.
  3. Modify the string object and call AppendLine of the StringBuilder (until the end of the file)
  4. Open a new StreamWriter, and write the StringBuilder to the write stream.

However, I've found that StremWriter.Write truncates 32768 bytes (2^16), but the length of StringBuilder is greater than that. I could write a simple loop to guarantee entire string to a file. But, I'm wondering what would be the most efficient way in C# for doing this task?

To summarize, I'd like to modify only some parts of a text file and write to a different file. But, the text file size could be larger than 32768 bytes.

== Answer == I'm sorry to make confusin to you! It was just I didn't call flush. StremWriter.Write does not have a short (e.g., 2^16) limitation.

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

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

发布评论

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

评论(5

陌伤浅笑 2024-10-25 03:12:05

该问题很可能与未关闭 writer 有关。请参阅 http://msdn.microsoft.com/en- us/library/system.io.streamwriter.flush.aspx

但我建议如果可以避免的话,不要将整个文件加载到内存中。

The problem is most likely related to not closing the writer. See http://msdn.microsoft.com/en-us/library/system.io.streamwriter.flush.aspx.

But I would suggest not loading the whole file in memory if that can be avoided.

唐婉 2024-10-25 03:12:05

你能试试这个吗:

    void Test()
    {
        using (var inputFile = File.OpenText(@"c:\in.txt"))
        {
            using (var outputFile = File.CreateText(@"c:\out.txt"))
            {
                string current;
                while ((current = inputFile.ReadLine()) != null)
                {
                    outputFile.WriteLine(Process(current));
                }
            }
        }
    }

    string Process(string current)
    {
        return current.ToLower();
    }

通过逐行处理并直接写入,可以避免将整个文件加载到内存中

can you try this :

    void Test()
    {
        using (var inputFile = File.OpenText(@"c:\in.txt"))
        {
            using (var outputFile = File.CreateText(@"c:\out.txt"))
            {
                string current;
                while ((current = inputFile.ReadLine()) != null)
                {
                    outputFile.WriteLine(Process(current));
                }
            }
        }
    }

    string Process(string current)
    {
        return current.ToLower();
    }

It avoid to have to full file loaded in memory, by processing line by line and writing it directly

给不了的爱 2024-10-25 03:12:05

嗯,这完全取决于您要修改的内容。如果您对文本文件的一部分的修改依赖于文本文件的另一部分,那么显然您需要将这两个部分都存储在内存中。但是,如果您只需要逐行修改文本文件,则使用如下所示的内容:

using (StreamReader sr = new StreamReader(@"test.txt"))
{
    using (StreamWriter sw = new StreamWriter(@"modifiedtest.txt"))
    {
        while (!sr.EndOfStream)
        {
            string line = sr.ReadLine();
            //do some modifications
            sw.WriteLine(line);
            sw.Flush(); //force line to be written to disk
        }
    }
}

Well, that entirely depends on what you want to modify. If your modifications of one part of the text file are dependent on another part of the text file, you obviously need to have both of those parts in memory. If however, you only need to modify the text file on a line-by-line basis then use something like this :

using (StreamReader sr = new StreamReader(@"test.txt"))
{
    using (StreamWriter sw = new StreamWriter(@"modifiedtest.txt"))
    {
        while (!sr.EndOfStream)
        {
            string line = sr.ReadLine();
            //do some modifications
            sw.WriteLine(line);
            sw.Flush(); //force line to be written to disk
        }
    }
}
落花随流水 2024-10-25 03:12:05

我不会使用洞文档来查找您正在寻找的内容,而是使用正则表达式来查找示例:

public List<string> GetAllProfiles()
    {
        List<string> profileNames = new List<string>();
        using (StreamReader reader = new StreamReader(_folderLocation + "profiles.pg"))
        {
            string profiles = reader.ReadToEnd();
            var regex = new Regex("\nname=([^\r]{0,})", RegexOptions.IgnoreCase);
            var regexMatchs = regex.Matches(profiles);
            profileNames.AddRange(from Match regexMatch in regexMatchs select regexMatch.Groups[1].Value);
        }
        return profileNames;
    }

Instead of of running though the hole dokument i would use a regex to find what you are looking for Sample:

public List<string> GetAllProfiles()
    {
        List<string> profileNames = new List<string>();
        using (StreamReader reader = new StreamReader(_folderLocation + "profiles.pg"))
        {
            string profiles = reader.ReadToEnd();
            var regex = new Regex("\nname=([^\r]{0,})", RegexOptions.IgnoreCase);
            var regexMatchs = regex.Matches(profiles);
            profileNames.AddRange(from Match regexMatch in regexMatchs select regexMatch.Groups[1].Value);
        }
        return profileNames;
    }
烂人 2024-10-25 03:12:04

StreamWriter.Write

不会

截断字符串,并且没有限制。

它在内部使用String.CopyTo,另一方面使用不安全代码(使用fixed)来复制字符,因此它是最高效。

StreamWriter.Write

does not

truncate the string and has no limitation.

Internally it uses String.CopyTo which on the other hand uses unsafe code (using fixed) to copy chars so it is the most efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文