使用 StreamWriter 将行附加到文件

发布于 2024-12-02 20:36:50 字数 257 浏览 0 评论 0原文

我想将行附加到我的文件中。我正在使用 StreamWriter:

StreamWriter file2 = new StreamWriter(@"c:\file.txt");
file2.WriteLine(someString);
file2.Close();

我的文件的输出应该是几个位于彼此下方的字符串,但我只有一行,每次运行此代码时都会被覆盖。

有什么方法可以让 StreamWriter 附加到现有文件吗?

I want to append lines to my file. I am using a StreamWriter:

StreamWriter file2 = new StreamWriter(@"c:\file.txt");
file2.WriteLine(someString);
file2.Close();

The output of my file should be several strings below each other, but I have only one row, which is overwritten every time I run this code.

Is there some way to let the StreamWriter append to an existing file?

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

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

发布评论

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

评论(11

心在旅行 2024-12-09 20:36:51

将此行: 替换

StreamWriter sw = new StreamWriter("c:/file.txt");

为以下代码:

StreamWriter sw = File.AppendText("c:/file.txt");

然后将您的行写入文本文件,如下所示:

sw.WriteLine("text content");

Replace this line:

StreamWriter sw = new StreamWriter("c:/file.txt");

with this code:

StreamWriter sw = File.AppendText("c:/file.txt");

and then write your line to the text file like this:

sw.WriteLine("text content");
尬尬 2024-12-09 20:36:51

你可以这样使用

 using (System.IO.StreamWriter file =new System.IO.StreamWriter(FilePath,true))
             {
                    
                    
            `file.Write("SOme Text TO Write" + Environment.NewLine);         
                    
             }
        

You can use like this

 using (System.IO.StreamWriter file =new System.IO.StreamWriter(FilePath,true))
             {
                    
                    
            `file.Write("SOme Text TO Write" + Environment.NewLine);         
                    
             }
        
浊酒尽余欢 2024-12-09 20:36:50

使用此替代:

new StreamWriter("c:\\file.txt", true);

通过 StreamWriter 构造函数的此重载,您可以选择是附加文件还是覆盖它。

C# 4 及更高版本提供以下语法,有些人认为该语法更具可读性:

new StreamWriter("c:\\file.txt", append: true);

Use this instead:

new StreamWriter("c:\\file.txt", true);

With this overload of the StreamWriter constructor you choose if you append the file, or overwrite it.

C# 4 and above offers the following syntax, which some find more readable:

new StreamWriter("c:\\file.txt", append: true);
天气好吗我好吗 2024-12-09 20:36:50
 using (FileStream fs = new FileStream(fileName,FileMode.Append, FileAccess.Write))
 using (StreamWriter sw = new StreamWriter(fs))
 {
    sw.WriteLine(something);
 }
 using (FileStream fs = new FileStream(fileName,FileMode.Append, FileAccess.Write))
 using (StreamWriter sw = new StreamWriter(fs))
 {
    sw.WriteLine(something);
 }
痴梦一场 2024-12-09 20:36:50

我假设您每次向文件写入内容时都会执行上述代码的全部。每次打开文件流时,其查找指针都会定位在开头,因此所有写入最终都会覆盖之前的内容。

您可以通过两种方式解决该问题:使用方便的方法

file2 = new StreamWriter("c:/file.txt", true);

或自己显式重新定位流指针:

file2 = new StreamWriter("c:/file.txt");
file2.BaseStream.Seek(0, SeekOrigin.End);

I assume you are executing all of the above code each time you write something to the file. Each time the stream for the file is opened, its seek pointer is positioned at the beginning so all writes end up overwriting what was there before.

You can solve the problem in two ways: either with the convenient

file2 = new StreamWriter("c:/file.txt", true);

or by explicitly repositioning the stream pointer yourself:

file2 = new StreamWriter("c:/file.txt");
file2.BaseStream.Seek(0, SeekOrigin.End);
北方的韩爷 2024-12-09 20:36:50

试试这个:

StreamWriter file2 = new StreamWriter(@"c:\file.txt", true);
file2.WriteLine(someString);
file2.Close();

Try this:

StreamWriter file2 = new StreamWriter(@"c:\file.txt", true);
file2.WriteLine(someString);
file2.Close();
预谋 2024-12-09 20:36:50

将其替换

StreamWriter file2 = new StreamWriter("c:/file.txt");

为:

StreamWriter file2 = new StreamWriter("c:/file.txt", true);

true 表示它附加文本。

Replace this:

StreamWriter file2 = new StreamWriter("c:/file.txt");

with this:

StreamWriter file2 = new StreamWriter("c:/file.txt", true);

true indicates that it appends text.

落在眉间の轻吻 2024-12-09 20:36:50

实际上,只有 Jon 的回答(2011 年 9 月 5 日 9:37)与 BaseStream.Seek 对我的案例有效。谢谢乔恩!我需要附加行到 zip 存档的 txt 文件。

using (FileStream zipFS = new FileStream(@"c:\Temp\SFImport\test.zip",FileMode.OpenOrCreate))
{
    using (ZipArchive arch = new ZipArchive(zipFS,ZipArchiveMode.Update))
    {
        ZipArchiveEntry entry = arch.GetEntry("testfile.txt");
        if (entry == null)
        {
            entry = arch.CreateEntry("testfile.txt");
        }
        using (StreamWriter sw = new StreamWriter(entry.Open()))
        {
            sw.BaseStream.Seek(0,SeekOrigin.End);
            sw.WriteLine("text content");
        }
    }
}

Actually only Jon's answer (Sep 5 '11 at 9:37) with BaseStream.Seek worked for my case. Thanks Jon! I needed to append lines to a zip archived txt file.

using (FileStream zipFS = new FileStream(@"c:\Temp\SFImport\test.zip",FileMode.OpenOrCreate))
{
    using (ZipArchive arch = new ZipArchive(zipFS,ZipArchiveMode.Update))
    {
        ZipArchiveEntry entry = arch.GetEntry("testfile.txt");
        if (entry == null)
        {
            entry = arch.CreateEntry("testfile.txt");
        }
        using (StreamWriter sw = new StreamWriter(entry.Open()))
        {
            sw.BaseStream.Seek(0,SeekOrigin.End);
            sw.WriteLine("text content");
        }
    }
}
我不会写诗 2024-12-09 20:36:50

使用带有第二个参数的StreamWriter构造函数 -

Use this StreamWriter constructor with 2nd parameter - true.

—━☆沉默づ 2024-12-09 20:36:50

另一种选择是使用 System.IO.File.AppendText

这相当于其他人给出的 StreamWriter 重载。

另外 File.AppendAllText 可能会提供稍微更简单的界面,而不必担心打开和关闭流。尽管您可能需要担心如何插入自己的换行符。 :)

Another option is using System.IO.File.AppendText

This is equivalent to the StreamWriter overloads others have given.

Also File.AppendAllText may give a slightly easier interface without having to worry about opening and closing the stream. Though you may need to then worry about putting in your own linebreaks. :)

泪痕残 2024-12-09 20:36:50

一种更简单的方法是使用 File.AppendText 它将 UTF-8 编码文本附加到现有文件,或者如果指定文件不存在则附加到新文件并返回 System.IO .StreamWriter

using (System.IO.StreamWriter sw = System.IO.File.AppendText(logFilePath + "log.txt"))
{                                                
    sw.WriteLine("this is a log");
}

One more simple way is using the File.AppendText it appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist and returns a System.IO.StreamWriter

using (System.IO.StreamWriter sw = System.IO.File.AppendText(logFilePath + "log.txt"))
{                                                
    sw.WriteLine("this is a log");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文