天天看點

System.IO全面總結(二)

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace StreamReader類

{

    class Program

    {

        //static void Main(string[] args)

        //{

        //}

        public static void Main()

        {

            string path = @"c: empMyTest.txt";

            try

            {

                if (File.Exists(path))

                {

                    File.Delete(path);

                }

                using (StreamWriter sw = new StreamWriter(path))//path 參數可以是檔案名,包括統一命名約定 (UNC) 共享上的檔案。如果此檔案已存在,将覆寫它;否則,将建立一個新檔案。

                {                                               //不要求 path 參數必須是存儲在磁盤上的檔案;它可以是系統中支援通過流進行通路的任何部分。

                    sw.WriteLine("This");  //WriteLine方法是繼承TextWriter基類的

                    sw.WriteLine("is some text");

                    sw.WriteLine("to test");

                    sw.WriteLine("Reading");

                using (StreamReader sr = new StreamReader(path))

                    while (sr.Peek() >= 0)  //Peek方法傳回下一個可用的字元,但不使用它。次方法的傳回值:下一個要讀取的字元,如果沒有更多的可用字元或此流不支援查找,則為 -1。

                    {

                        Console.WriteLine(sr.ReadLine());   //ReadLine方法是重寫基類TextRead的          

                    }

            }

            catch (Exception e)

                Console.WriteLine("The process failed: {0}", e.ToString());

        }

    }

}

/*

StreamReader.ReadLine 方法 :從目前流中讀取一行字元并将資料作為字元串傳回。此方法的傳回值:輸入流中的下一行;如果到達了輸入流的末尾,則為空引用(在VB中為Nothing,C#中為null)。

 ----繼承關系:

  System.Object

   System.MarshalByRefObject

    System.IO.TextReader

       System.IO.StreamReader

       System.IO.StringReader

    System.IO.TextWriter

       System.CodeDom.Compiler.IndentedTextWriter

       System.IO.StreamWriter

       System.IO.StringWriter

       System.Web.HttpWriter

       System.Web.UI.HtmlTextWriter

StreamReader的構造函數必須要求指定檔案必須已經存在,如果不存在則會發生異常

StreamWriter的構造函數不要求指定的檔案必須已經存在,如果不存在則會建立該檔案,如果存在則會改寫或者追加,這要看你用那一個構造函數而定

StreamReader中的Close,Peek,Read,ReadLine,ReadToEnd都是重寫基類TextRead中的虛拟方法

StreamWriter中的Close,Flush,Write是重寫基類TextWriter中的虛拟方法,而WriteLine是繼承基類TextWriter(虛拟的)中的方法

一般在用完一個流之後要用Close關閉這個流,例如StreamReader和StreamWriter

*/

//using System;

//using System.IO;

//using System.Text;

//class Test

//{

//    public static void Main()

//    {

//        string path = @"c: empMyTest.txt";

//        try

//        {

//            // Delete the file if it exists.

//            if (File.Exists(path))

//            {

//                // Note that no lock is put on the

//                // file and the possibliity exists

//                // that another process could do

//                // something with it between

//                // the calls to Exists and Delete.

//                File.Delete(path);

//            }

//            // Create the file.

//            using (FileStream fs = File.Create(path))

//                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");

//                // Add some information to the file.

//                fs.Write(info, 0, info.Length);

//            // Open the stream and read it back.

//            using (StreamReader sr = File.OpenText(path)) //File.OpenText (string Path)//傳回類型為SteamReader,打開現有UTF-8編碼文本檔案以進行讀取

//                string s = "";

//                while ((s = sr.ReadLine()) != null)

//                {

//                    Console.WriteLine(s);

//                }

//        }

//        catch (Exception Ex)

//            Console.WriteLine(Ex.ToString());

//    }

//}

//class Test

//    public static void Main()

//        if (!File.Exists(path))

//            // Create a file to write to.

//            using (StreamWriter sw = File.CreateText(path))

//                sw.WriteLine("Hello");

//                sw.WriteLine("And");

//                sw.WriteLine("Welcome");

//            }   

//        // Open the file to read from.

//        using (StreamReader sr = File.OpenText(path))

//            string s = "";

//            while ((s = sr.ReadLine()) != null)

//                Console.WriteLine(s);

繼續閱讀