What is difference between FileStream and StreamWriter in .NET?
What context are you supposed to use it? What is their advantage and disadvantage?
Is it possible to combine these two into one?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A
FileStreamis aStream. Like all Streams it only deals withbyte[]data.A
StreamWriter : TextWriteris a Stream-decorator. A TextWriter encodes the primitive types like string, int and char tobyte[]and then writes that to the linked Stream.You use a bare FileStream when you have
byte[]data. You add aStreamWriterwhen you want to write text. Use a Formatter or a Serializer to write more complex data.Yes. You always need a Stream to create a StreamWriter. The helper method
System.IO.File.CreateText("path")will create them in combination and then you only have to Dispose() the outer writer.