c# stream类相关测试续(二)

附上前期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键就退出来了");

        }
    }
}

请使用浏览器的分享功能分享到微信等