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
28
votes
4 answers

Write a string to a file

I want to write something to a file. I found this code: private void writeToFile(String data) { try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE)); …
jason
  • 6,962
  • 36
  • 117
  • 198
27
votes
3 answers

How To Overwrite A File If It Already Exists?

I'm making a music player. It has 2 forms; one is the main area where you play music. The second form has a CheckedListBox where you select the mp3s you want. When I click a button, it saves the selection in a .txt file so I can access them in the…
Manny
  • 325
  • 1
  • 3
  • 10
26
votes
2 answers

StreamWriter end line with Lf rather than CrLf

I'm using StreamWriter to output a text file, but I would like to end my WriteLine with Lf rather than the default CrLf. Is there an easy way to do this using a parameter built into StreamWriter? I feel like quite a moron right now, but I'll leave…
Alec Sanger
  • 4,442
  • 1
  • 33
  • 53
25
votes
2 answers

how to overwrite data in a txt file?

Possible Duplicate: Can any one tell why the previous data is still displayed while saving data using StreamWriter I have WPF C# application, that reads and writes to a .txt file, i know how to write line but line, but how do I overwrite the text…
Beef
  • 1,413
  • 6
  • 21
  • 36
23
votes
2 answers

c# replace string within file

String.Replace doesn't seem to work properly when replacing a portion of an HTML file's content. For example, String.Replace replaces with blah blah blah html> - notice the second HTML closing tag is not properly…
Joey
  • 231
  • 1
  • 2
  • 3
22
votes
3 answers

CsvHelper wrap all values with quotes

I am using CsvHelper I need to wrap all values with quotes. Is that possible? Data = is a List using (StreamWriter textWriter = new StreamWriter(path)) { textWriter.BaseStream.Write(p, 0, p.Length); // var dt = new DataTable(); …
Ovi
  • 2,459
  • 9
  • 50
  • 72
22
votes
2 answers

StreamWriter and IFormatProvider

How do I pass an IFormatProvider to a StreamWriter? Specifically I want to create a new StreamWriter("myfile.txt", CultureInfo.InvariantCulture); TextWriter and StringWriter have a parameter for that in the constructor, but StreamWriter does…
HugoRune
  • 13,157
  • 7
  • 69
  • 144
21
votes
7 answers

How to get full path of StreamWriter

I'm creating a StreamWriter using a relative path. But the file doesn't appear. To troubleshoot, I want to check that the full path is what I'm expecting. So having a StreamWriter instance, how can I get the full path of the file it's going to write…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
20
votes
3 answers

FileStream and StreamWriter - How to truncate the remainder of the file after writing?

var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); using(var writer = new StreamWriter(fs)) writer.Write(....); If the file previously contained text and the newly-written text is shorter than what was already in…
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
20
votes
1 answer

Why should asyncio.StreamWriter.drain be explicitly called?

From doc: write(data) Write data to the stream. This method is not subject to flow control. Calls to write() should be followed by drain(). coroutine drain() Wait until it is appropriate to resume writing to the stream.…
balki
  • 26,394
  • 30
  • 105
  • 151
19
votes
6 answers

With StreamWriter doesn't work \n (C#)

I have a problem with the C# Stream Writer. I use the following Code: //Constructor public EditorTXTFile { FileStream f = File.Create(System.IO.Directory.GetCurrentDirectory() + "\\Output.txt"); f.Close(); } //Function AddText public void…
Waronius
  • 363
  • 2
  • 4
  • 10
18
votes
8 answers

Exporting datagridview to csv file

I'm working on a application which will export my DataGridView called scannerDataGridView to a csv file. Found some example code to do this, but can't get it working. Btw my datagrid isn't databound to a source. When i try to use the Streamwriter to…
PandaNL
  • 828
  • 5
  • 18
  • 40
18
votes
4 answers

What is the difference between calling Stream.Write and using a StreamWriter?

What is the difference between instantiating a Stream object, such as MemoryStream and calling the memoryStream.Write() method to write to the stream, and instantiating a StreamWriter object with the stream and calling streamWriter.Write()? Consider…
John Nelson
  • 5,041
  • 6
  • 27
  • 34
18
votes
5 answers

Streamwriter is cutting off my last couple of lines sometimes in the middle of a line?

Here is my code. : FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None); FileStream fileStreamWrite = new FileStream(reProcessedFile, FileMode.OpenOrCreate, FileAccess.Write,…
Bill Blankenship
  • 3,316
  • 6
  • 43
  • 73
17
votes
5 answers

StringBuilder.ToString() throws OutOfMemoryException

I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException. StringBuilder SB = new StringBuilder(); for(int i =0; i<=5000; i++) { SB.Append("Some Junk Data for…
Thiru
  • 3,293
  • 7
  • 35
  • 52