泛型_文件流

1.泛型

    优点:使用泛型类型可以最大限度地重用代码、保护类型的安全以及提高性能。

     实际开发中,一般方法或API等使用的是数组作为规范,但是实际的使用都是使用集合。


声明一个数组:类型[] 变量 = {};

数组的类型是int[];

T[] 变量;//泛型


(1) List<T> 线性集合,长度可变的数组    T指类型

-> 增 Add() AddRange()
  -> 删 Remove(T)    RemoveAt(int)
  -> 改 可以像数组一样修改   // list[9] = '123';
  -> 查 bool Contains(T)
    IndexOf()
  -> 遍历 for
    foreach(类型 临时变量 in 集合)
    {
     使用临时变量进行处理
    }
    
  -> 模拟队列:先进先出
   list.Add() 进入
   list[i]
   list.ReamoveAt(0)
  -> 模拟栈:先进后出(杯子与奥利奥)
   list.Add() 进入
   list[list.Count - 1]
   list.RemoveAt(list.Count - 1)



(2)Dictionary<TKey ,TValue> 散列集合

Directory 静态
   string[] GetFiles(string) // 返回指定路径下的所有文件
   string[] GetDirectories(string) // 返回改路径下的所有子文件夹
   bool Exist() //判断文件夹是否存在


-> 键值对
    为了快速查找
  -> 常用方法
   -> 增 Add(键, 值)
   -> 删 RemoveKey
   -> 改 索引   dic["dictionary"] = "词典";
   -> 查 索引
     ContainsKey
  -> 遍历(不常用)
   foreach(KeyValuePair<TKey, TValue> item in dic)
   {
       
   }// 键值对集合,没有顺序


使用

 // 词典
            Dictionary<string, string> dic = new Dictionary<string, string>();
            // 初始化
            dic.Add("sound", "声音,合乎逻辑的");
            dic.Add("late", "迟到的,过去的,已故的人或最近发生的事");
            dic.Add("tell", "告诉,电话,可分辨出,可理解的");
 while (true)
            {
                Console.WriteLine("请输入要查的单词");
                string input = Console.ReadLine();
                if (dic.ContainsKey(input))
                {
                    Console.WriteLine(dic[input]);
                }
                else
                {
                    Console.WriteLine("词典库未收录该单词,请联系。。。。");
                }
                Console.ReadKey();
            }



2.Encoding(不懂)

参考资料http://www.cnblogs.com/criedshy/archive/2012/08/08/2628554.html    C# 小叙 Encoding (一二三)


3.文件流FileStream

文件:就是一堆字节块

使用:引用命名空间system.IO  //input ,output

(1) FileStream 文件流: 将文件以字节的方式进行读写

-> 构造方法:new FileStream(文件路径, FileMode, FileAccess);
    FileMode:Create、Append、Open
    -> 读写方法:
    -> int ReadByte();  void WriteByte(byte);
    ->  int Read(byte[] buffer, int offset, int count);
     void Write(byte[] buffer, int offset, int count);
   -> 了解
    -> void Flush(); // 清空缓存,写入硬盘
    -> void Dispose(); // 清空非托管资源
    -> Position属性 // 文件处理,指针与文本文件书写一样是有位置的


(2)StreamReader与StreamWriter: 专门针对文本文件的读写操作 

    -> 读流:
    new StreamReader("文件路径", Encoding);
   -> 写流
    new StreamWriter("文件路径", bool isAppend, Encoding);
   -> 读的方法
    string ReadLine(); // 读取文件中的一行
    string ReadToEnd(); // 读取所有的内容
   -> 写的方法
    void Write(...);  // 写入内容
    void WriteLine(...);  // 写入一行文本


(3)File静态

File.AppendAllText
   AppendAllLines
    ReadAllText
   ReadAllLines
   WriteAllText
   WriteAllLines
   bool Exist()


(4)-> Path 处理路径
   获得后缀名
   获得文件名
   获得没有后缀名的文件名
   获得全路径
  -> FileInfo DirectoryInfo DriverInfo

FileInfo相当于File静态的实例版本 

DirectoryInfo 相当于FileInfo静态的实例版本

DriverInfo硬盘的容量是多少



>>FileStream的写

  using (FileStream fWriter = new FileStream("txt.txt", FileMode.Create, FileAccess.Write))//实际应用开发中比较复杂,故用using括起来
            {
                //
                // fWriter.WriteByte(97);
                string s = Console.ReadLine();
                // byte[] bs = Encoding.Default.GetBytes(s);

                // ff fe
                fWriter.WriteByte(255);
                fWriter.WriteByte(254);

                byte[] bs = Encoding.Unicode.GetBytes(s);

                for (int i = 0; i < bs.Length; i++)
                {
                    fWriter.WriteByte(bs[i]);
                }

            }



3.读写流

Console.Write("输入文件名:");
            string file = Console.ReadLine();

            using (StreamWriter writer = new StreamWriter(file, true, Encoding.Default))
            {
                // 写文本了
                // jkexit
                while (true)
                {
                    Console.Write("jk@itcast.cn/user/jk>");
                    string input = Console.ReadLine();

                    if (input.ToLower() == "jkexit")
                    {
                        Console.WriteLine("保存成功");
                        Console.ReadKey();
                        break;
                    }

                    // 文本写入文件
                    writer.WriteLine(input);
                }
            } 

  using (StreamReader reader = new StreamReader("wifi密码破解程序小计.txt", Encoding.Default))
            {
                // Console.BackgroundColor = ConsoleColor.Blue;
                string str = null;
                int i = 0;
                while ((str = reader.ReadLine()) != null)
                {
                    Console.WriteLine("{0}\t{1}", ++i ,str);
                }
            }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值