如何使用 StreamWriter 重写文件或追加到文件

发布于 2024-12-04 12:34:12 字数 340 浏览 0 评论 0原文

我正在使用此代码:

for ($number=0; $number < 5; $number++) {
    StreamWriter x = new StreamWriter("C:\\test.txt");
                 x.WriteLine(number);
                 x.Close();
}

如果 test.txt 中有某些内容,此代码不会覆盖它。我有两个问题:

  1. 如何让它覆盖文件?
  2. 如何附加到同一个文件?

I am using this code:

for ($number=0; $number < 5; $number++) {
    StreamWriter x = new StreamWriter("C:\\test.txt");
                 x.WriteLine(number);
                 x.Close();
}

If something is in test.txt, this code will not overwrite it. I have two questions:

  1. How can I make it overwrite the file?
  2. How can I append to the same file?

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

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

发布评论

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

评论(5

酒中人 2024-12-11 12:34:12

StreamWriter 的默认行为是创建一个新文件,或者覆盖它(如果存在)。要附加到文件,您需要使用接受布尔值并将其设置为 true 的重载。在您的示例代码中,您将重写 test.txt 5 次。

using(var sw = new StreamWriter(@"c:\test.txt", true))
{
    for(int x = 0; x < 5; x++)
    {
        sw.WriteLine(x);    
    }
}

StreamWriter's default behavior is to create a new file, or overwrite it if it exists. To append to the file you'll need to use the overload that accepts a boolean and set that to true. In your example code, you will rewrite test.txt 5 times.

using(var sw = new StreamWriter(@"c:\test.txt", true))
{
    for(int x = 0; x < 5; x++)
    {
        sw.WriteLine(x);    
    }
}
西瑶 2024-12-11 12:34:12

尝试使用 FileMode 枚举器:

// will append to end of file
FileStream fappend = File.Open("C:\\test.txt", FileMode.Append); 

// will create the file or overwrite it if it already exists
FileStream fcreate = File.Open("C:\\test.txt", FileMode.Create); 

Try the FileMode enumerator:

// will append to end of file
FileStream fappend = File.Open("C:\\test.txt", FileMode.Append); 

// will create the file or overwrite it if it already exists
FileStream fcreate = File.Open("C:\\test.txt", FileMode.Create); 
难忘№最初的完美 2024-12-11 12:34:12

您可以将第二个参数传递给 StreamWriter启用禁用附加到文件:

using System.IO;

// This will enable appending to file.
StreamWriter stream = new StreamWriter("YourFilePath", true);

// This is default mode, not append to file and create a new file.
StreamWriter stream = new StreamWriter("YourFilePath", false);

You can pass a second parameter to StreamWriter to enable or disable appending to file:

using System.IO;

// This will enable appending to file.
StreamWriter stream = new StreamWriter("YourFilePath", true);

// This is default mode, not append to file and create a new file.
StreamWriter stream = new StreamWriter("YourFilePath", false);
憧憬巴黎街头的黎明 2024-12-11 12:34:12

您可以首先使用 FileStream,然后将其传递给 StreamWriter。

FileStream fsOverwrite = new FileStream("C:\\test.txt", FileMode.Create);
StreamWriter swOverwrite = new StreamWriter(fsOverwrite);

或者

FileStream fsAppend = new FileStream("C:\\test.txt", FileMode.Append);    
StreamWriter swAppend = new StreamWriter(fsAppend);

You can start by using the FileStream and then passing that to your StreamWriter.

FileStream fsOverwrite = new FileStream("C:\\test.txt", FileMode.Create);
StreamWriter swOverwrite = new StreamWriter(fsOverwrite);

or

FileStream fsAppend = new FileStream("C:\\test.txt", FileMode.Append);    
StreamWriter swAppend = new StreamWriter(fsAppend);
2024-12-11 12:34:12

那么你的代码的结果是什么?

我希望该文件只包含数字 4,因为默认行为是创建/覆盖,但您是说它没有覆盖?

您应该能够通过执行您正在执行的操作来使其覆盖文件,并且可以通过使用 FileMode.Append 创建 FileStream 来进行追加。

So what is the result of your code?

I would expect the file to contain nothing but the number 4, since the default behavior is to create/overwrite, but you are saying that it is not overwriting?

You should be able to make it overwrite the file by doing what you are doing, and you can append by making a FileStream with FileMode.Append.

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