Questions tagged [streamwriter]

StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

In .Net StreamWriter (under System.IO) is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding) or StreamWriter.

By default, a StreamWriter is not thread safe
See official documentation from Microsoft.

1524 questions
16
votes
3 answers

Add a new line at a specific position in a text file.

I am trying to add a specific line of text in a file. Specifically between two boundaries. An example of what it would look like if I wanted to add a line in between the boundaries of item1: [item1] 2550 coins 995 200000 7 2550 coins 995 200000…
Simon Taylor
  • 503
  • 2
  • 7
  • 17
15
votes
4 answers

simultaneous read-write a file in C#

I have a file containing data that I'd like to monitor changes to, as well as add changes of my own. Think like "Tail -f foo.txt". Based on this thread, it looks like I should just create a filestream, and pass it both to a writer and reader. …
tbischel
  • 6,337
  • 11
  • 51
  • 73
15
votes
6 answers

Try/Finally block vs calling dispose?

Is there any difference between these two code samples and if not, why does using exist? StreamWriter writer; try { writer = new StreamWriter(...) writer.blahblah(); } finally { writer.Dispose(); } vs: using (Streamwriter writer = new…
rollsch
  • 2,518
  • 4
  • 39
  • 65
14
votes
3 answers

What is the purpose of StreamReader when Stream.Read() exists?

This has been bugging me. I know Stream is an abstract class and therefore can't be instantiated but it has classes that are derived from it. Why is there a StreamReader class and a Stream.Read() method (and vice verse for StreamWriter and…
Bagofsheep
  • 159
  • 1
  • 1
  • 8
14
votes
6 answers

Adding a Line to the Middle of a File with .NET

Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example: Hello my name is…
Brandon
  • 141
  • 1
  • 1
  • 4
13
votes
4 answers

Shortest way to save DataTable to Textfile

I just found a few answers for this, but found them all horribly long with lots of iterations, so I came up with my own solution: Convert table to string: string myTableAsString = String.Join(Environment.NewLine,…
Leonardo Trivino
  • 295
  • 1
  • 4
  • 11
13
votes
2 answers

What consumes less resources and is faster File.AppendText or File.WriteAllText storing first in StringBuilder?

I have to write thousands of dynamically generated lines to a text file. I have two choices, Which consumes less resources and is faster than the other? A. Using StringBuilder and File.WriteAllText StringBuilder sb = new…
Alberto León
  • 2,879
  • 2
  • 25
  • 24
13
votes
6 answers

Remove all previous text before writing

I am writing a text file and each time i write i want to clear the text file. try { string fileName = "Profile//" + comboboxSelectProfile.SelectedItem.ToString() + ".txt"; using (StreamWriter sw = new…
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
12
votes
6 answers

Write Unicode String In a File Using StreamWriter doesn't Work

I have this code: string s = "آ"; StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8); writer.WriteLine(s); but when I run it I can't see any "آ" in a.txt!! There isn't any string in a.txt! It is Empty! What is problem!?! Can…
ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91
12
votes
5 answers

Getting the file size from StreamWriter

using (var writer = File.CreateText(fullFilePath)) { file.Write(fileContent); } Given the above code, can the file size be known from StreamWriter?
Moh Moh Oo
  • 269
  • 1
  • 3
  • 19
12
votes
3 answers

C# Unit Test a StreamWriter parameter

I have a bunch of classes that all implement an Interface and one of the parameters is a StreamWriter. I need to check the contents of the StreamWriter. I am trying to find a way to avoid writing text files on the test server and opening them to…
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42
12
votes
1 answer

How can I tell if a streamwriter is closed?

I'm using a streamwriter in combination with a background worker, for logging. As such, I have System::Void MyUI::execBWorker_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) { String^ outputPath =…
Melanie
  • 1,349
  • 2
  • 17
  • 27
11
votes
3 answers

Overwrite contents of ZipArchiveEntry

How can I overwrite contents of a ZipArchiveEntry? Following code using StreamWriter with StringBuilder fails if the new file contents are shorter than the original ones, for example: using System.IO.Compression; //... using (var archive =…
wondra
  • 3,271
  • 3
  • 29
  • 48
11
votes
3 answers

Why does file extension affect write speed? (C#, StreamWriter)

I am currently testing the performance of different methods for logging text data into a file. It seems that when I open/write/close a large amount of times, the extension used affects the performance. (.txt and .log are ~7 times faster) Code…
11
votes
2 answers

StreamReader and Portable Class Library

I am writing a ConfigManager class using Portable Class Libraries. PCL supports StreamReader and StreamWriter classes that I want to use, but the PCL version of those classes do not support passing in a string during construction. PCL also does not…