IO

1.内存流

  1. string source = "this is a source string";
  2. MemoryStream ms = new MemoryStream(100);
  3. ms.Write(ASCIIEncoding.ASCII.GetBytes(source), 0, source.Length);
  4. Cosole.WriteLine(string.Format("Capacity:{0}/nLenght:{1}/nPosition:{2}", ms.Capacity, ms.Length, ms.Position));
  5. ms.Seek(0, SeekOrigin.Begin);
  6. byte[] tempBytes = new byte[source.Length];
  7. ms.Read(tempBytes, 0, 4);
  8. Console.WriteLine(ASCIIEncoding.ASCII.GetString(tempBytes));
  9. ms.Close();

 

2.文件IO

2.1添加数据

  1. //method 1
  2. string str = "This is a string";
  3. FileStream fs = new FileStream("test.txt", FileMode.Create);
  4. fs.write(ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length);
  5. fs.Close();
  6. //method 2
  7. StreamWriter sw = FileCreateText("test.txt");
  8. sw.Write(str);
  9. sw.Close();

 

2.2读数据

  1. //method 1
  2. byte[] tempBytes = new byte[str.Length];
  3. FileStream fs = new FileStream("test.txt", FileMode.Open);
  4. fs.Read(tempBytes, 0, (int)fs.Length);
  5. Console.WriteLine(ASCIIEncoding.ASCII.GetString(tempBytes));
  6. fs.Close();
  7. //method 2
  8. StreamReader sr = File.OpenText("test.txt");
  9. Console.WriteLine(sr.ReadToEnd());
  10. sr.Close();

 

3.使用文件和目录系统

File类:静态类

FileInfo类:实例类

Directory类:静态类

DirectoryInfo类:实例类

Path类:分析和构造路径字符串的实用类

FileSecurity类:获得访问控制列表

  1. FileInfo f = new FileInfo("test.txt");
  2. FileSecurity fs = f.GetAccessControl();
  3. Console.WriteLine("Owner: {0}", fs.GetOwner(typeof(NTAccount)));

 

4.异步文件IO

5.独立存储

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值