---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
集合:
ArrayList 集合:
可以看作是大小可按需求动态增加的数组。
Insert()方法:
集合名.Insert(位置, 值);
Remove方法
集合名.Remove(数据)
RemoveAt方法
删掉指定索引出的数据,其后数据依次向前移动
当索引超出范围后,异常
判断是否包含:Contains()
例如:
// 随即生成10个1-100之间的数放到ArrayList中,要求这个10个数不能重复,并且都是偶数
Random r = new Random();
ArrayList array = new ArrayList(10);
for (int i = 0; i < 10; i++)
{
int rNumber = r.Next(1, 101);
if (!array.Contains(rNumber))
{
if (rNumber % 2 == 0)
{
array.Add(rNumber);
}
else
{
i--;
}
}
else
{
i--;
}
}
for (int i = 0; i < array.Count; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
哈希表(Hashtable):
用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key/value键值对均为object类型,所以Hashtable可以支持任何类型的key/value键值对。
泛型集合:
List<T>泛型集合:
例如:
//把一个int数组中的奇数放到一个新的int数组中返回
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> oddList = new List<int>();
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] % 2 != 0)
{
oddList.Add(numbers[i]);
}
}
//将泛型集合转换数组 ToArray()
int[] odd = oddList.ToArray();//要求你的泛型集合必须是int类型的。
for (int i = 0; i < odd.Length; i++)
{
Console.WriteLine(odd[i]);
}
Console.ReadKey();
Dictionary<TKey,TValue>泛型集合:
例如:
//计算字符串中每种字符出现的次数
string str = "welcomwe to china ";
Dictionary<char, int> dic = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ' ')
{
continue;
}
if (!dic.ContainsKey(str[i]))
{
dic[str[i]] = 1;
}
else
{
dic[str[i]]++;
}
}
foreach (KeyValuePair<char,int> item in dic )
{
Console.WriteLine("字母{0}出现了{1}次",item.Key,item.Value);
}
Console.ReadKey();
文件管理:
File类:
File.Copy(“source”, “targetFileName”, true);//文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。
File.Exists();//判断文件是否存在
File.Move(“source”, “target”);//移动(剪切),思考如何为文件重命名?
File.Delete(“path”);//删除。如果文件不存在?不存在,不报错
File.Create(“path”);//创建文件
File.Copy(“source”, “targetFileName”, true);//文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。
File.Exists();//判断文件是否存在
File.Move(“source”, “target”);//移动(剪切)。
File.ReadAllLines(“path”, Encoding.Default);//读取所有行,返回string[]
File.ReadAllText(“path”, Encoding.Default);//读取所有文本返回string
File.ReadAllBytes(“path”);//读取文件,返回byte[],把文件作为二进制来处理。
File.WriteAllLines(“path”, new string[4] ,Encoding.Default);//将string数组按行写入文件。
File.WriteAllText(“path”, “string”);//将字符串全部写入文件
File.WriteAllBytes(“path”,new byte[5]);//将byte[]全部写入到文件
File.AppendAllText()//将string追加到文件
File.ReadAllLines(“path”, Encoding.Default);//读取所有行,返回string[]
File.ReadAllText(“path”, Encoding.Default);//读取所有文本返回string
File.ReadAllBytes(“path”);//读取文件,返回byte[],把文件作为二进制来处理。
File.WriteAllLines(“path”, new string[4] ,Encoding.Default);//将string数组按行写入文件。
File.WriteAllText(“path”, “string”);//将字符串全部写入文件
File.WriteAllBytes(“path”,new byte[5]);//将byte[]全部写入到文件
File.AppendAllText()//将string追加到文件
File.ReadAllLines(“path”, Encoding.Default);//读取所有行,返回string[]
File.ReadAllText(“path”, Encoding.Default);//读取所有文本返回string
File.ReadAllBytes(“path”);//读取文件,返回byte[],把文件作为二进制来处理。
File.WriteAllLines(“path”, new string[4] ,Encoding.Default);//将string数组按行写入文件。
File.WriteAllText(“path”, “string”);//将字符串全部写入文件
File.WriteAllBytes(“path”,new byte[5]);//将byte[]全部写入到文件
File.AppendAllText()//将string追加到文件
File.Copy(“source”, “targetFileName”, true);//文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。
File.Exists();//判断文件是否存在
File.Move(“source”, “target”);//移动(剪切),思考如何为文件重命名?
File.Delete(“path”);//删除。如果文件不存在?不存在,不报错
File.Create(“path”);//创建文件
File.Copy(“source”, “targetFileName”, true);//文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。
File.Exists();//判断文件是否存在
File.Move(“source”, “target”);//移动(剪切),思考如何为文件重命名?
File.Delete(“path”);//删除。如果文件不存在?不存在,不报错
File.Create(“path”);//创建文件
File.Copy(“source”, “targetFileName”, true);//文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。
File.Exists();//判断文件是否存在
File.Move(“source”, “target”);//移动(剪切),思考如何为文件重命名?
File.Delete(“path”);//删除。如果文件不存在?不存在,不报错
File.Create(“path”);//创建文件
File.Copy(“source”, “targetFileName”, true);//文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。
File.Exists();//判断文件是否存在
File.Move(“source”, “target”);//移动(剪切),思考如何为文件重命名?
File.Delete(“path”);//删除。如果文件不存在?不存在,不报错
File.Create(“path”);//创建文件
目录管理:
Directory 类:
1)创建文件夹 Directory.CreateDirectory(路径);
2)判断是否存在 Directory.Exist(路径);
3)Delete删除 两个参数 一个是路径 另一个为true或者false
默认为false,如果要删除的文件夹下有子文件夹或者文件,如果你还要删除,加一个true
4)GetFiles()获取文件夹下所有的文件名 两个参数 一个是路径,另一个要搜索的文件的后缀名“*.....”;
5)GetDirecotries() 获得文件夹下所有的子文件夹
文件流:
File可以读写文件,多用来读写小文件,而FileStream和Stream多用来读写大文件。
FileStream也可以读写文件(对字节进行操作)
1、创建对象
2、创建缓冲区的大小
3、开始读取
Stream 又分为StreamReader和StreamWriter也可以读写文件。(对字符进行操作,只能操作文本)
注:将创建FileStream对象和读写操作的代码写在Using中,再最后,我们就不需要手动的close和dispose,因为using会帮助咱们自动的close和dispose。
例1:
//使用FileStream读取文件
//1、创建FileStream对象
FileStream fsReader = new FileStream("1.txt", FileMode.Open, FileAccess.Read);
//缓冲区
byte[] bufferOld = new byte[fsReader.Length];
//实际读到的数据
int r = fsReader.Read(bufferOld, 0, bufferOld.Length);
//将字节数组转换成字符串
string str = System.Text.Encoding.Default.GetString(bufferOld);
fsReader.Close();//关闭流
fsReader.Dispose();//释放所占用的资源
Console.WriteLine(str);
Console.ReadKey();
例2:
//使用FileStream来写数据
//创建FileStream的对象
FileStream fsWrite = new FileStream("1.txt", FileMode.Append, FileAccess.Write);
string strNew = "djffsjdflsdlfjsdlgkjlsjdgs";
byte[] bufferNew=System.Text.Encoding.Default.GetBytes(strNew);
fsWrite.Write(bufferNew, 0, bufferNew.Length);
fsWrite.Close();
fsWrite.Dispose();
Console.WriteLine("写入OK");
Console.ReadKey();
----------------------ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------