C# 创建文本文件写入读取实现代码
C# 创建文本文件写入读取实现代码
发布时间:2016-12-28 来源:查字典编辑
摘要:第一次运行时:第二次运行时:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usi...

第一次运行时:

C# 创建文本文件写入读取实现代码1

第二次运行时:

C# 创建文本文件写入读取实现代码2

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace 文件操作

{

class Program

{

static void Main(string[] args)

{

//创建一个文本文件,最好先判断一下

StreamWriter sw;

if (!File.Exists("templog.txt"))

{

//不存在就新建一个文本文件,并写入一些内容

sw = File.CreateText("templog.txt");

sw.Write("第一个字");

sw.WriteLine(" 跟随老大的.");

sw.WriteLine("当前日期是:");

sw.WriteLine(DateTime.Now);

}

else

{

//如果存在就添加一些文本内容

sw = File.AppendText("templog.txt");

for (int i = 0; i < 10; i++)

{

sw.WriteLine("可以像平时输出到屏幕一样输出{0}", i);

}

}

sw.Close();

//创建一个读取器

StreamReader sr = new StreamReader("templog.txt");

//一次性读取完

Console.WriteLine(sr.ReadToEnd());

Console.ReadLine();

}

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类