1.内存流
- string source = "this is a source string";
- MemoryStream ms = new MemoryStream(100);
- ms.Write(ASCIIEncoding.ASCII.GetBytes(source), 0, source.Length);
- Cosole.WriteLine(string.Format("Capacity:{0}/nLenght:{1}/nPosition:{2}", ms.Capacity, ms.Length, ms.Position));
- ms.Seek(0, SeekOrigin.Begin);
- byte[] tempBytes = new byte[source.Length];
- ms.Read(tempBytes, 0, 4);
- Console.WriteLine(ASCIIEncoding.ASCII.GetString(tempBytes));
- ms.Close();
2.文件IO
2.1添加数据
- //method 1
- string str = "This is a string";
- FileStream fs = new FileStream("test.txt", FileMode.Create);
- fs.write(ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length);
- fs.Close();
- //method 2
- StreamWriter sw = FileCreateText("test.txt");
- sw.Write(str);
- sw.Close();
2.2读数据
- //method 1
- byte[] tempBytes = new byte[str.Length];
- FileStream fs = new FileStream("test.txt", FileMode.Open);
- fs.Read(tempBytes, 0, (int)fs.Length);
- Console.WriteLine(ASCIIEncoding.ASCII.GetString(tempBytes));
- fs.Close();
- //method 2
- StreamReader sr = File.OpenText("test.txt");
- Console.WriteLine(sr.ReadToEnd());
- sr.Close();
3.使用文件和目录系统
File类:静态类
FileInfo类:实例类
Directory类:静态类
DirectoryInfo类:实例类
Path类:分析和构造路径字符串的实用类
FileSecurity类:获得访问控制列表
- FileInfo f = new FileInfo("test.txt");
- FileSecurity fs = f.GetAccessControl();
- Console.WriteLine("Owner: {0}", fs.GetOwner(typeof(NTAccount)));
4.异步文件IO
5.独立存储