using (var writer = File.CreateText(fullFilePath))
{
file.Write(fileContent);
}
Given the above code, can the file size be known from StreamWriter
?
Specifically, a FileStream exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes. A StreamWriter is a wrapper for a Stream that simplifies using that stream to output plain text.
The StreamReader and StreamWriter classes are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.
StreamWriter. WriteLine() method writes a string to the next line to the steam. The following code snippet creates and writes different author names to the stream.
Yes, you can, try the following
long length = writer.BaseStream.Length;//will give unexpected output if autoflush is false and write has been called just before
Note: writer.BaseStream.Length
property can return unexpected results since StreamWriter
doesn't write immediately. It caches so to get expected output you need AutoFlush = true
writer.AutoFlush = true; or writer.Flush();
long length = writer.BaseStream.Length;//will give expected output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With