C#學習筆記---區分StringWriter(Reader)和StreamWriter(Reader),TextWriter(Reader),BinaryWriter(Reader)

來源:互聯網
上載者:User

標籤:.exe   iter   binary   擷取對象   creat   toe   sys   XML   type   

1.TextWriter(Reader)分別是對連續字元系列處理的編寫器(讀寫器),來自System.IO

2.StringWriter(Reader)繼承TextWriter(Reader),它主要是對字串進行處理的類,範例如下:

            TextWriter stringwriter = new StringWriter();
            stringwriter.Write("你好,通過StringWriter寫入,StringWriter繼承TextWriter");
            TextReader stringreader = new StringReader(stringwriter.ToString());
            Console.WriteLine("從{0}——{1}", stringreader.GetType().Name, stringreader.ReadToEnd());
            Console.WriteLine();

3.StreamWriter(Reader)也繼承TextWriter(Reader),它主要是以流的形式處理資料,可以是記憶流,檔案流,系列連接埠流以及媒體視頻流等。例子:

            using (TextWriter streamwriter = new StreamWriter("mytext.txt"))
            {
                streamwriter.Write("你好,通過StreamWriter寫入,StreamWriter繼承TextWriter");
            }
            TextReader streamreader = new StreamReader("mytext.txt");
            Console.WriteLine("從{0}——{1}", streamreader.GetType().Name, streamreader.ReadToEnd());
            Console.ReadLine();

4.BinaryWriter(Reader)不繼承TextWriter(Reader),以二進位形式講基元類型寫入(讀取),並支援特定的編碼,其有廣泛的支援形式,因為它以二進位方式讀取寫入。例子:

            //BinaryWriter(Reader)
            const string filename = "test.txt";
            if (File.Exists(filename))
            {
                Console.WriteLine("{0}檔案已存在!", filename);
            }
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            string str = Environment.CurrentDirectory + @"QQ.EXE.lnk";
            bw.Write(str);
            bw.Close();
            fs.Close();
            //現在開始讀取
            fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long size = br.BaseStream.Length;
            byte[] bytesize = new byte[size];
            bytesize = br.ReadBytes((int)size);
            Console.WriteLine(bytesize.ToString());
            br.Close();
            fs.Close();
            Console.ReadLine();

下面再示一例BinaryWriter寫入XML資訊例子:

            const string filename = "XmlWriter1.xml";
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            XmlElement ee = xdoc.DocumentElement;
            if (ee != null)
            {
                bw.Write(ee.OuterXml);
            }
            else
            {
                Console.WriteLine("擷取對象失敗!");
            }
            bw.Close();
            fs.Close();

C#學習筆記---區分StringWriter(Reader)和StreamWriter(Reader),TextWriter(Reader),BinaryWriter(Reader)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.