手机
当前位置:查字典教程网 >编程开发 >C#教程 >C# 创建文本文件写入读取实现代码
C# 创建文本文件写入读取实现代码
摘要:第一次运行时:第二次运行时:复制代码代码如下: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#从实体对象集合中导出Excel的代码

C#绝对路径拼接相对路径的实例代码

C#泛型约束的深入理解

c# n个数排序实现代码

C# 邮件发送和接收实现代码

C#拼接SQL语句 用ROW_NUMBER实现的高效分页排序

c# 托盘双击不触发单击事件的实现方法

C#版ftp方法实现类的代码

C#词法分析器之输入缓冲和代码定位的应用分析

C#生成注册码的实例代码

精品推荐
分类导航