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

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

using System;
using System.IO; namespace IO
{
class Program
{
static void Main(string[] args)
{
// 判断文件是否存在
Console.WriteLine(File.Exists(@"C:\Users\ASUS\Desktop\memo\a.txt"));
// 判断目录是否存在
Console.WriteLine(Directory.Exists(@"C:\"));
// 下面例子将展示如何查找某一目录下所有exe文件的信息
// . 表示当前目录
string path = ".";
if (args.Length > )
{
// 如果需要在其他目录执行则打开控制行,之后进入项目下的Debug目录,见下图
path = args[];
}
else
{
Console.WriteLine("Directory not found");
} DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo f in dir.GetFiles("*.exe"))
{
string name = f.Name;
long size = f.Length;
DateTime creationTime = f.CreationTime;
Console.WriteLine(name);
Console.WriteLine(size);
Console.WriteLine(creationTime);
Console.WriteLine("------------");
}
}
}
}

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

2.写入文件

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "a.txt";
static void Main(string[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("already exists.");
return;
} FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs); for(int i = ; i < ; i++)
{
w.Write("a");
}
w.Close();
fs.Close();
}
}
}

 

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

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "a.txt";
static void Main(string[] args)
{
using(StreamWriter w = File.AppendText("test.txt"))
{
Log("Hi,is me.", w);
Log("how are u", w);
w.Close();
} }
// 方法,用于写入数据
public static void Log(string logMessage,TextWriter w)
{
w.Write("\r\nLog Entry");
w.WriteLine(":{0}", logMessage);
w.Flush(); }
}
}

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

2.读取文件

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "a.txt";
static void Main(string[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
return;
}
// 路径,操作类别,权限
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
       // 读取前5个字符
for(int i = ; i < ; i++)
{
Console.WriteLine(r.ReadString());
}
r.Close();
fs.Close();
} }
}

完整读取某一文件:

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "test.txt";
static void Main(string[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
return;
}
using(StreamReader sr = File.OpenText(FILE_NAME))
{
string input;
while((input = sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine("ended");
sr.Close();
}
} }
}

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. tp3中子查询 逻辑条件是or

    直接用写sql最快 $map['_string'] = 'status=1 AND score>10'; //子查询条件字段不同 $condition['platform'] = 'swap'; ...

  2. 《走出软件作坊》//TODO

    目录 简介 结束语 简介 作者吕建伟(@阿朱),研发管理专家,原京东技术学院院长,中国互联网技术联盟发起人,历任首席架构师.技术总监以及CTO等职位.目前已接受用友集团董事长王文京邀请,加入用友组建研 ...

  3. 005.前端开发知识,前端基础CSS(2020-01-14)

    一.CSS权重 权重是可以叠加的,事例如下: div ul li ------> 0,0,0,3 .nav ul li ------> 0,0,1,2 a:hover -----—> ...

  4. python语法基础-并发编程-线程-长期维护

    ###############   线程和GIL,全局解释器锁    ############## """ 线程 为什么会有进程? 主要是能够同时处理多个任务,多个任务还 ...

  5. rest framework-restful介绍-长期维护

    ###############   django框架-rest framework    ############### # django rest framework 框架 # 为什么学习这个res ...

  6. iOS运营级B2B服务平台App、自定义图标库、个人中心页面、识别身份证Demo、瀑布流等源码

    iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... iOS可自定义图表库 - PNChart 开源一款曾是运营级的B2B服务 ...

  7. 奇点云数据中台技术汇(四)| DataSimba系列之流式计算

    你是否有过这样的念头:如果能立刻马上看到我想要的数据,我就能更好地决策?   市场变化越来越快,企业对于数据及时性的需求,也越来越大,另一方面,当下数据容量呈几何倍暴增,数据的价值在其产生之后,也将随 ...

  8. IntelliJ IDEA 的便捷操作性

    快捷键 说明 IntelliJ IDEA 的便捷操作性,快捷键的功劳占了一大半,对于各个快捷键组合请认真对待.IntelliJ IDEA 本身的设计思维是提倡键盘优先于鼠标的,所以各种快捷键组合层出不 ...

  9. SRA|GEO|Taxonomy|Pubmed|MeSH|EBI|Uniprot|Human project|Ensembl|UCSC

    生物医学大数据: SRA:Sequence Read Archive (SRA) makes biological sequence data available to the research co ...

  10. awk使用和详解

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...