StreamReader与StreamWriter

来源:互联网 发布:将文件夹共享到网络 编辑:程序博客网 时间:2024/04/27 17:16

using System;
using System.IO;
namespace file
{
    class Program
    {       
        public static void Main(string[] args)
        {
            string path1=@"d:/file/file1.txt";
            using(StreamWriter sw=new StreamWriter(path1))
            {
                sw.Write("文本文件");
                sw.WriteLine("的写入/读取示例:");
                sw.WriteLine("--------------------------------");
                sw.WriteLine("写入整数{0}或浮点数{1}",1,4.2);
                bool b=false ;
                char grade ='A';
                string s="Multiple Data Type!";
                sw.WriteLine("写入Boolean 值、字符、字符串、日期:");
                sw.WriteLine(b);
                sw.WriteLine(grade);
                sw.WriteLine(s);
                sw.Write("当前日期为:");
                sw.WriteLine(DateTime.Now);               
            }
            try
            {
                using(StreamReader sr=new StreamReader(path1))
                {
                    string line;
                    while((line=sr.ReadLine())!=null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("该文件不能正常读取:");
                Console.WriteLine(e.Message);               
            }
            Console.ReadLine();           
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

原创粉丝点击