TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# FileStream Example, File.Create

Use FileStream and File.Create to create a file. Another object like StreamWriter can be used with FileStream.
FileStream. In the .NET Framework we often access files through streams. We can place one stream on top of another (usually with "using" statements).File
This is powerful. For the FileStream, we often need to use another stream (like StreamWriter) on top of it. We can do many things with a file using a FileStream.
An example. Here we get a FileStream using the File.Create method. Other methods, like File.Open or File.OpenText can be used to get a FileStream.File.Open

StreamWriter: We pass the FileStream object we get from File.Create to a StreamWriter. We then use WriteLine to write to the file.

StreamWriter
C# program that uses FileStream, File.Create using System; using System.IO; class Program { static void Main() { // Use FileStream with File.Create. // ... Pass FileStream to a StreamWriter and write data to it. using (FileStream fileStream = File.Create(@"C:\programs\example1.txt")) using (StreamWriter writer = new StreamWriter(fileStream)) { writer.WriteLine("Example 1 written"); } Console.WriteLine("DONE"); } } Output DONE Contents of example1.txt: Example 1 written
Some notes. It is important to understand how streams can be used together to develop complex (and custom) file handling routines in C#. Many stream types are available.

And: Often the FileStream serves as a "base" to other streams we might use. The example above shows this.

Stream types, methods. Streams can act on a physical file (as with FileStream) or a logical piece of memory (as with MemoryStream). We can act upon these streams in a similar way.StreamMemoryStreamBaseStreamFileStream Length
A brief summary. Understanding the dynamics of streams is important. For more complex tasks, a FileStream is often useful as many streams are combined in methods, one acting upon another.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf