天天看點

FileStream、StreamWriter、StreamReader操作文本檔案

用FileStream對象讀取字元

using System;
using System.IO;

namespace StreamRead
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FileStream fs = new FileStream("Log.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string strLine;
                // Read data in line by line.
                while ((strLine = sr.ReadLine()) != null)
                {
                    Console.WriteLine(strLine);
                }
                sr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(ex.ToString());
                return;
            }
            Console.ReadKey();
        }
    }
}
           

用FileStream對象将字元寫入文本文本

using System;
using System.IO;

namespace WriteFile
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] byData;
            char[] charData;

            try
            {
                FileStream fs = new FileStream("temp.txt", FileMode.Create);
                charData = "My pink half of the drainpipe.".ToCharArray();
                byData = new byte[charData.Length];
                Encoder e = Encoding.UTF8.GetEncoder();
                e.GetBytes(charData, 0, charData.Length, byData, 0, true);

                // Move file pointer to beginning of the file
                fs.Seek(0, SeekOrigin.Begin);
                fs.Write(byData, 0, byData.Length);
            }
            catch (IOException ex)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
                return;
            }
        }
    }
}
           

用StreamReader對象讀取字元

using System;
using System.IO;

namespace StreamRead
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FileStream fs = new FileStream("Log.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string strLine;
                // Read data in line by line.
                while ((strLine = sr.ReadLine()) != null)
                {
                    Console.WriteLine(strLine);
                }
                sr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(ex.ToString());
                return;
            }
            Console.ReadKey();
        }
    }
}
           

用StreamWritter對象将字元寫入文本檔案

using System;
using System.IO;

namespace StreamWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FileStream fs = new FileStream("Log.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);

                bool truth = true;
                // Write data to file
                sw.WriteLine("Hello to you.");
                sw.WriteLine("It's now {0} and things are looking good.", DateTime.Now.ToLongTimeString());
                sw.Write("More than that, ");
                sw.Write("It's {0} that C# is fun.", truth);
                sw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An IO exception has been thrown!");
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
                return;
            }
        }
    }
}
           

繼續閱讀