c# stream類相關測試續(二)

wisdomone1發表於2012-03-28
附上前期stream示例
http://space.itpub.net/9240380/viewspace-706058

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            //streamwriter實現了抽象基類textwriter
            //try
            //{
            //    //學習filestream寫入writebyte(單位元組)與write(位元組陣列)
            //    FileStream f1 = new FileStream(@"c:\testfilestream.txt", FileMode.Append);
            //    f1.WriteByte(0);
            //    f1.WriteByte(1);
            //    byte[] b1 = new byte[5] { 1, 2, 3, 4, 5 };
            //    f1.Write(b1, 0, 3);
            //    f1.Close();
            //}
            //catch(IOException ix)
            //{
            //    Console.WriteLine(ix.Message);
            //    Console.ReadKey();
                          
            //}
            //try
            //{
            //    //filemode.append只能與fileaccess的read匹配
            //    FileStream fs = new FileStream(@"c:\mvp.txt", FileMode.CreateNew, FileAccess.Write);
            //    int i = 1;
            //    do
            //    {
            //        //streamwriter是以位元組流方面寫入
            //        //StreamWriter sw = new StreamWriter(fs);
            //        TextWriter tw = new StreamWriter(fs);
            //        tw.Write("第一次資料寫入到檔案" + Convert.ToChar(i));
            //        i++;
            //    } while (i < 11);//do while後面要分號
            //    fs.Close();
               

            //}
            //catch(IOException ic)
            //{
            //    Console.WriteLine(ic.Message);
               
               
               
            //}

            //finally
            //{
            //    Console.WriteLine("streamwriter工作完畢");
            //    //Console.ReadKey();
           
            //}


            //學習streamwriter向文字檔案寫入資料 filemode.append只能與fileaccess.read匹配
            //FileStream fs = new FileStream(@"c:\mvp.txt", FileMode.Open, FileAccess.Read);
           
            //try
            //{
            //    //streamwriter建構函式的異常
            //    // 異常:
            //    //   System.ArgumentNullException:
            //    //     stream 為 null。
            //    //
            //    //   System.ArgumentException:
            //    //     stream 不可寫。
            //    StreamWriter sw = new StreamWriter(fs);
            //    sw.WriteLine("haha");
            //    sw.Write("sex");
            //    sw.Close();
            //}
            //    //建構函式引數為空
            //catch(ArgumentNullException exp)
            //{
            //    Console.WriteLine(exp.Message);
            //    return;
            //}
            //catch(ArgumentException exp)
            //{
            //    Console.WriteLine(exp.Message);
            //    Console.ReadKey();
            //    return;
            //}
            //fs.Close();

            //學習streamreader把檔案內容讀取顯示出來
            // FileMode: Append 與 FileAccess: Read 的組合無效。 提示此錯誤 argumentexception異常
            //FileStream fs1 = new FileStream(@"c:\mvp.txt", FileMode.Open, FileAccess.Read);
            //StreamReader sr = new StreamReader(fs1);
            //while (sr.Peek()!=-1)
            //{
            //   string readstr=sr.ReadLine();
            //   Console.WriteLine(readstr);
              
            //}
            //Console.ReadKey();
            //sr.Close();

            ////file.createtext返回streamwriter型別
            //StreamWriter strwr = File.CreateText(@"c:\mvp.txt");
            ////createtext重寫(無建立或重寫已存在檔案)
            //strwr.Write("透過mtoto.createtext寫入文字");
            //strwr.Close();


            //學習readkey方法,它返回consolekeyinfo型別
            //readkey是與鍵盤互動最好的方式,不是採用行緩衝
            char cc;
           
            {
                ConsoleKeyInfo ck1 = Console.ReadKey();
                if (ck1.KeyChar.ToString()=="")
                {
                    ConsoleKey ck = ck1.Key;
                    if (ck == ConsoleKey.Tab)
                        Console.WriteLine("按下鍵盤鍵" + ConsoleKey.Tab.ToString());
                   
                   
                }
                else
                {
                    Console.WriteLine(ck1.KeyChar);
                    Console.ReadKey();
                }

               
               
            }
            //ConsoleKey.Console.WriteLine("按了q鍵就退出來了");

        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-719749/,如需轉載,請註明出處,否則將追究法律責任。

相關文章