Read and Write a Text File : Text File Read Write « File Stream « C# / C Sharp






Read and Write a Text File

     

using System;
using System.IO;
using System.Text;

public class TextFileTest {

    private static void Main() {
        FileStream fs = new FileStream("test.txt", FileMode.Create);

        StreamWriter w = new StreamWriter(fs, Encoding.UTF8);

        w.WriteLine(1.2M);
        w.WriteLine("string");
        w.WriteLine('!');
        w.Flush();
        w.Close();
        fs.Close();

        fs = new FileStream("test.txt", FileMode.Open);
        StreamReader r = new StreamReader(fs, Encoding.UTF8);

        Console.WriteLine(Decimal.Parse(r.ReadLine()));
        Console.WriteLine(r.ReadLine());
        Console.WriteLine(Char.Parse(r.ReadLine()));

        r.Close();
        fs.Close();
    }
}

   
    
    
    
  








Related examples in the same category

1.Read text file line by line
2.Read ASCII string from byte bufferRead ASCII string from byte buffer
3.Reads and displays bytes until end-of-fileReads and displays bytes until end-of-file
4.Write string to a text file
5.Read whole text file to the end
6.Read text file line by line with exception catch
7.Text file Write with format and write boolean value to a text file
8.Action Text Reader Line
9.Open and Append to a Log File
10.Read and Write to a Newly Created Data File
11.Read Text from a File
12.Read text file with File.OpenText
13.Write Text to a File
14.Read a text file and obtain it's contents.
15.Reads text from a file.
16.Saves the text to a file.
17.Read Text File From Ressource
18.Create a large file of 100 lines to upload
19.Creates or opens a file for writing and writes text to it.
20.Returns the raw number of the current line count.
21.Returns the zero-based line number where source appears in target.
22.Returns the number of lines appearing in target where a line is counted as a '\n'