1. 讲解ASP.net MVC的I/O操作

新建一个控制台程序,输入代码如下

  1. using System;
  2. using System.IO;
  3.  
  4. namespace IO
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. // 判断文件是否存在
  11. Console.WriteLine(File.Exists(@"C:\Users\ASUS\Desktop\memo\a.txt"));
  12. // 判断目录是否存在
  13. Console.WriteLine(Directory.Exists(@"C:\"));
  14. // 下面例子将展示如何查找某一目录下所有exe文件的信息
  15. // . 表示当前目录
  16. string path = ".";
  17. if (args.Length > )
  18. {
  19. // 如果需要在其他目录执行则打开控制行,之后进入项目下的Debug目录,见下图
  20. path = args[];
  21. }
  22. else
  23. {
  24. Console.WriteLine("Directory not found");
  25. }
  26.  
  27. DirectoryInfo dir = new DirectoryInfo(path);
  28. foreach(FileInfo f in dir.GetFiles("*.exe"))
  29. {
  30. string name = f.Name;
  31. long size = f.Length;
  32. DateTime creationTime = f.CreationTime;
  33. Console.WriteLine(name);
  34. Console.WriteLine(size);
  35. Console.WriteLine(creationTime);
  36. Console.WriteLine("------------");
  37. }
  38. }
  39. }
  40. }

File,Directory为一个静态的Class,无法实例化。

2.写入文件

  1. using System;
  2. using System.IO;
  3.  
  4. namespace IO
  5. {
  6. class Program
  7. {
  8. private const string FILE_NAME = "a.txt";
  9. static void Main(string[] args)
  10. {
  11. if (File.Exists(FILE_NAME))
  12. {
  13. Console.WriteLine("already exists.");
  14. return;
  15. }
  16.  
  17. FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
  18. BinaryWriter w = new BinaryWriter(fs);
  19.  
  20. for(int i = ; i < ; i++)
  21. {
  22. w.Write("a");
  23. }
  24. w.Close();
  25. fs.Close();
  26. }
  27. }
  28. }

 

如果文件已存在,我们需要覆盖内容到里面怎么办?

  1. using System;
  2. using System.IO;
  3.  
  4. namespace IO
  5. {
  6. class Program
  7. {
  8. private const string FILE_NAME = "a.txt";
  9. static void Main(string[] args)
  10. {
  11. using(StreamWriter w = File.AppendText("test.txt"))
  12. {
  13. Log("Hi,is me.", w);
  14. Log("how are u", w);
  15. w.Close();
  16. }
  17.  
  18. }
  19. // 方法,用于写入数据
  20. public static void Log(string logMessage,TextWriter w)
  21. {
  22. w.Write("\r\nLog Entry");
  23. w.WriteLine(":{0}", logMessage);
  24. w.Flush();
  25.  
  26. }
  27. }
  28. }

using 内代码执行完毕后会自动释放资源,常用于读写文件以及连接数据库

2.读取文件

  1. using System;
  2. using System.IO;
  3.  
  4. namespace IO
  5. {
  6. class Program
  7. {
  8. private const string FILE_NAME = "a.txt";
  9. static void Main(string[] args)
  10. {
  11. if (!File.Exists(FILE_NAME))
  12. {
  13. Console.WriteLine("{0} does not exist!",FILE_NAME);
  14. return;
  15. }
  16. // 路径,操作类别,权限
  17. FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
  18. BinaryReader r = new BinaryReader(fs);
           // 读取前5个字符
  19. for(int i = ; i < ; i++)
  20. {
  21. Console.WriteLine(r.ReadString());
  22. }
  23. r.Close();
  24. fs.Close();
  25. }
  26.  
  27. }
  28. }

完整读取某一文件:

  1. using System;
  2. using System.IO;
  3.  
  4. namespace IO
  5. {
  6. class Program
  7. {
  8. private const string FILE_NAME = "test.txt";
  9. static void Main(string[] args)
  10. {
  11. if (!File.Exists(FILE_NAME))
  12. {
  13. Console.WriteLine("{0} does not exist!",FILE_NAME);
  14. return;
  15. }
  16. using(StreamReader sr = File.OpenText(FILE_NAME))
  17. {
  18. string input;
  19. while((input = sr.ReadLine())!=null)
  20. {
  21. Console.WriteLine(input);
  22. }
  23. Console.WriteLine("ended");
  24. sr.Close();
  25. }
  26. }
  27.  
  28. }
  29. }

MVC07的更多相关文章

  1. 快速入门系列--MVC--07与HTML5移动开发的结合

    现在移动互联网的盛行,跨平台并兼容不同设备的HTML5越来越盛行,很多公司都在将自己过去的非HTML5网站应用渐进式的转化为HTML5应用,使得一套代码可以兼容不同的物理终端设备和浏览器,极大的提高了 ...

  2. MVC-07 案例1

    >>>>>ContosoUniversity网站 ------------------------------------------- 一.并发冲突 1. 为什么会并发 ...

  3. MVC-07 案例2

    二.电子商务网站 掌握该网站的开发流程和设计思路,并为数据模型中商品.商品分类,这两个类编写代码. 1.需求分析 2.数据模型规划 (1)商品类别 (2)商品信息 (3)会员信息 (4)购物车项目 ( ...

  4. MVC-07数据库

    部分6:添加数据库. 创建数据库 Visual Web Developer带有免费的SQL数据库,名为SQL Server Compact. 数据库创建: 1.右键点击解决方案资源管理器中的App_D ...

  5. 快速入门系列--MVC--01概述

    虽然使用MVC已经不少年,相关技术的学习进行了多次,但是很多技术思路的理解其实都不够深入.其实就在MVC框架中有很多设计模式和设计思路的体现,例如DependencyResolver类就包含我们常见的 ...

  6. 快速入门系列--MVC--02路由

    现在补上URL路由的学习,至于蒋老师自建的MVC小引擎和相关案例就放在论文提交后再实践咯.通过ASP.NET的路由系统,可以完成请求URL与物理文件的分离,其优点是:灵活性.可读性.SEO优化.接下来 ...

随机推荐

  1. 系统学习javaweb3----HTML语言3(结束)

    说明:昨天是北方小年,需要做的事情有点多,需要祭灶,扫尘.包饺子,吃糖瓜儿,学习时间有点少,所以今天将两天的知识综合一下发出. 自我感觉:虽然感觉大致都了解了HTML语言,但是感觉自己面对程序还是无从 ...

  2. Tript协议|伯尔尼公约|著作权|立法宗旨|自动保护|著作权集体管理|

    知识产权 国际条约: Tript协议是国际性公约,<与贸易有关的知识产权协定>(英文:Agreement on Trade-Related Aspects of Intellectual ...

  3. 信息检索盛会 微软“领衔主演”——记ACM SIGIR 2013信息检索国际会议

    微软"领衔主演"--记ACM SIGIR 2013信息检索国际会议" title="信息检索盛会 微软"领衔主演"--记ACM SIGIR  ...

  4. 微信小程序的标签和html标签比较

    html 小程序 <div></div> <view></view> <h1><h2>....<h6> <p& ...

  5. Prefix and Suffix

    题目描述 Snuke is interested in strings that satisfy the following conditions: The length of the string ...

  6. c++与c语言的区别部分

    1.new       <malloc> delete    <free> 2.多态: 重载 <函数     操作符>   类似于c中的变化参数 虚函数 3.模板 ...

  7. 奇点云数据中台技术汇(一) | DataSimba——企业级一站式大数据智能服务平台

    在这个“数据即资产”的时代,大数据技术和体量都有了前所未有的进步,若企业能有效使用数据,让数据赚钱,这必将成为企业数字化转型升级的有力武器. 奇点云自研的一站式大数据智能服务平台——DataSimba ...

  8. ACG记录整理

    资料来源 日文维基百科 bangumi番组计划 中文维基百科 百度百科 豆瓣电影 资料类型 テレビアニメ‎ OVA‎ アニメ映画‎ Webアニメ‎ 内容说明 番名,带超链接介绍,尽量选用国内网站介绍, ...

  9. kafka spark steam 写入elasticsearch的部分问题

    应用版本 elasticsearch 5.5 spark 2.2.0 hadoop 2.7 依赖包版本 docker cp /Users/cclient/.ivy2/cache/org.elastic ...

  10. cs231n spring 2017 lecture14 Reinforcement Learning

    (没太听明白,下次重新听) 1. 增强学习 有一个 Agent 和 Environment 交互.在 t 时刻,Agent 获知状态是 st,做出动作是 at:Environment 一方面给出 Re ...