Static methods for text file

File class provides the static methods CreateText, AppendText, and OpenText to shortcut the process:


using System;
using System.IO;
using System.Linq;
class Program
{
    static void Main()
    {
        using (TextWriter writer = File.CreateText("test.txt"))
        {
            writer.WriteLine("Line1");
            writer.WriteLine("Line2");
        }

        using (TextWriter writer = File.AppendText("test.txt"))
            writer.WriteLine("Line3");

        using (TextReader reader = File.OpenText("test.txt")){
            while (reader.Peek() > -1){
                Console.WriteLine(reader.ReadLine()); 
            }    
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.