Questions tagged «streamwriter»

10
使用StreamWriter将行附加到文件
我想在文件中附加行。我正在使用StreamWriter: StreamWriter file2 = new StreamWriter(@"c:\file.txt"); file2.WriteLine(someString); file2.Close(); 我的文件的输出应该是彼此下面的几个字符串,但是我只有一行,每次运行此代码时,该行都会被覆盖。 有什么方法可以让StreamWriter追加到现有文件吗?
170 c#  streamwriter 

5
我应该为流对象调用Close()还是Dispose()吗?
类如Stream,StreamReader,StreamWriter等工具IDisposable界面。这意味着,我们可以Dispose()对这些类的对象调用方法。他们还定义了public一种称为的方法Close()。现在,让我感到困惑的是,一旦完成对象我该怎么称呼?如果我都叫我怎么办? 我当前的代码是这样的: using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { using (StreamWriter writer = new StreamWriter(filename)) { int chunkSize = 1024; while (!reader.EndOfStream) { char[] buffer = new char[chunkSize]; int count = reader.Read(buffer, 0, chunkSize); if (count != 0) { writer.Write(buffer, 0, count); } } …


8
如果文件不存在,则创建文件
如果文件不存在,我需要让我的代码读取以创建else追加。现在正在读取是否确实存在创建和附加。这是代码: if (File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { 我会这样做吗? if (! File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { 编辑: string path = txtFilePath.Text; if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { foreach (var line in employeeList.Items) { sw.WriteLine(((Employee)line).FirstName); sw.WriteLine(((Employee)line).LastName); sw.WriteLine(((Employee)line).JobTitle); } } } else { StreamWriter …
76 c#  streamwriter 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.