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
6
votes
3 answers

How to write to a file in Unicode in Vb.Net

How should I modify the following Vb.Net code to write str to the file in unicode? Do I need to convert str to Unicode before writing to the file? Using sw As StreamWriter = New StreamWriter(fname) sw.Write(str) sw.Close() End Using
CJ7
  • 22,579
  • 65
  • 193
  • 321
6
votes
4 answers

Console.SetOut to StreamWriter, write to textfile constantly

I'm using the Console.SetOut method to write all my Console.Out.WriteLines to a file, and this works. The only problem is that it only writes everything to the textfile when I close my application instead of it writing whenever a…
6
votes
2 answers

StreamWriter not writing to file when called from task Scheduler C#

I have the following function, that accepts a string, and logs the string content to a log file. private static void LogEvent(string sEvent) { sEvent = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "|" + sEvent; …
neildt
  • 5,101
  • 10
  • 56
  • 107
6
votes
1 answer

Why is my text file incomplete after using StreamWriter object?

I wrote a small program which generates big text files. I found using a StreamWriter is much, much faster than other methods I know. But the end of each text file is missing. I reduced the program to a very simple snippet to spot the problem, but…
Gregory MOUSSAT
  • 832
  • 4
  • 13
  • 22
6
votes
1 answer

reading and modifying large text files 3-5GB

I have a rather large file consisting of several million lines and there is the need to check and remove corrupt lines from the file. I have shamelessly tried File.ReadAllLines but it didn't work. Then I tried to stream lines as below reading from…
mechanicum
  • 699
  • 3
  • 14
  • 25
6
votes
4 answers

C# Write to multiple files without constantly closing/reopening streams. SteamWriter?

I am trying to read in a table from an Access database, and then sort the data in that table out to a number of text files. The clincher is that the filename to write to is dependant upon values in each record. This is officially my first C#…
Daniel Ward
  • 103
  • 1
  • 1
  • 5
6
votes
1 answer

StreamWriter replace line with a new text

Is it possible to replace the text in a text file with a new text without erasing the other data, here is my sample code, but its not working, I know there's a problem with it but I can't figure out, thanks, private void button1_Click_1(object…
Pyromancer
  • 2,429
  • 5
  • 19
  • 28
6
votes
3 answers

How does BufferedWriter work in java

I frequently output text to files. I wonder something: how does BufferedWriterwork? Does it write text on file when I call writer.write(text)? If it doesn't write text,do I need to use flush function to write data? For example: File file =…
RockOnGom
  • 3,893
  • 6
  • 35
  • 53
6
votes
6 answers

Which is the most performance based method of writing logs for Application

I am working in a client server application in which multiple clients and server are working on socket based communication for financial transactions where performance is very critical. Currently I am using streamwriter of system.IO namespace to…
funsukvangdu
  • 1,621
  • 4
  • 20
  • 33
6
votes
3 answers

Java - Writing to txt in a JAR file

Possible Duplicate: How can a Java program use files inside the .jar for read and write? How do I write to a .txt file from a JAR java compiled project? When I run my projects, it doesn't give errors, but it just doesn't write to the .txt inside…
Koen Demonie
  • 539
  • 1
  • 7
  • 24
6
votes
2 answers

Double flush required?

Is it necessary to flush a Stream after flushing a StreamWriter? public static async Task WriteStringAsync(this Stream stream, string messageString) { var encoding = new UTF8Encoding(false); //no BOM using (var streamWriter = new…
spender
  • 117,338
  • 33
  • 229
  • 351
6
votes
2 answers

C# Asp.Net Create text file, zip it, and save to Blob - Without writing anything to disk

A complicated one here, well for me anyway :) Basically what i would like to achieve is to generate some text, zip this text file within two directories and then upload it to a MySQL blob field - all without writing anything to the disk. I am…
JazziJeff
  • 721
  • 4
  • 17
  • 35
5
votes
9 answers

C# add line numbers to a text file

I am trying to read a text file in C# and add line numbers to the lines. This my input file: This is line one this is line two this is line three And this should be the output: 1 This is line one 2 this is line two 3 this is…
Vojtech
  • 643
  • 3
  • 16
  • 30
5
votes
1 answer

DataGridView to CSV File

I have a VB 2010 Express Project, that has a DataGridView in it, that I am trying to write to a CSV file. I have the write all working. But its slow. Slow = maybe 30 seconds for 6000 rows over 8 columns. Here is my code: Private Sub…
greg
  • 333
  • 2
  • 3
  • 9
5
votes
1 answer

c# can i create a dynamic file name with streamwriter?

I was trying to make the program write to a file that was named with a time stamp. Basically, saving a timestamp to a string value, I wanted it to create the file based on that time stamp. For example "Flight Manifest 10/14/2010 1:38:29…
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176