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

        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-719749/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9240380/viewspace-719749/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值