首页 > 代码库 > 集合与文件操作

集合与文件操作

今天在云和学院学习了集合和文件操作

 

ArrayList的添加、移除、清除

            string[] strs = { "阳光路上", "如果爱", "玫瑰花的葬礼", "超级英雄" };            string str = "love";            int i = 1;            double d = 10.21;            float f = 1.2f;            ArrayList arraylist = new ArrayList();            arraylist.Add(str);            arraylist.Add(i);            arraylist.Add(d);            arraylist.Add(f);            arraylist.AddRange(strs);            arraylist.Remove(1);            arraylist.RemoveAt(3);            arraylist.Clear();            foreach (var item in arraylist)            {                Console.WriteLine(item);            }            Console.ReadKey();

 Hashtable

            Hashtable table = new Hashtable();            table.Add("1", "宋祖英");            table.Add("2", "凤凰传奇");            table.Add("3", "何炅");            foreach (var item in table.Keys)            {                Console.WriteLine(item);            }
  Console.ReadKey();

  List:泛型集合

            List<int> list = new List<int>() { 12, 45, 3, 78 };            foreach (var item in list)            {                Console.WriteLine(item);            }             Console.ReadKey();

 Dictionary

 Dictionary<string, int> dictionary = new Dictionary<string, int>();            dictionary.Add("李谷一",1);            dictionary.Add("牛莉",2);            dictionary.Add("伊能静",3);            foreach (var item in dictionary.Keys)            {                Console.WriteLine(item);            }            Console.ReadKey();

 文件操作:File

命名空间:using System.IO;

创建文件:

string path = @"d:11.txt"; File.CreateText(path);            Console.WriteLine("创建文件");            Console.ReadKey();

 Path

string str = @"E:\\KwDownload\";            string strs = "song\\S.H.E-美丽新世界.mp3";            Console.WriteLine(Path.Combine(str,strs));            string path = @"E:\KwDownload\song\S.H.E-美丽新世界.mp3";            Console.WriteLine(Path.GetExtension(path));            Console.WriteLine(Path.GetFileName(path));            Console.WriteLine(Path.GetFileNameWithoutExtension(path));            Console.WriteLine(Path.GetFullPath(path));            Console.WriteLine(Path.GetDirectoryName(path));            string s = @"d:\11.txt";            Console.WriteLine(Path.ChangeExtension(s,"doc"));            Console.ReadKey();

 Path.Combine:合并路径

Path.GetExtension:取得扩展名

Path.GetFileName:取得文件名

Path.GetFullPath:取得完整的路径

Path.ChangeExtension:改变扩展名

集合与文件操作