前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#读取文本文件和C# 写文本文件

C#读取文本文件和C# 写文本文件

作者头像
阳光岛主
发布2019-02-19 12:34:08
3.4K0
发布2019-02-19 12:34:08
举报
文章被收录于专栏:米扑专栏米扑专栏

C#读取文本文件

今天一个学生问我如何从一个文本中读取内容,如下是做的是控制台中的例子,在别的地方也是这个道理。

// 读操作         public static void Read()         {   // 读取文件的源路径及其读取流             string strReadFilePath = @"../../data/ReadLog.txt";             StreamReader srReadFile = new StreamReader(strReadFilePath);

// 读取流直至文件末尾结束             while (!srReadFile.EndOfStream)             {                 string strReadLine = srReadFile.ReadLine(); //读取每行数据                 Console.WriteLine(strReadLine); //屏幕打印每行数据             }

// 关闭读取流文件             srReadFile.Close();             Console.ReadKey();         }

===================================================================

C# 写文本文件

// 写操作         public static void Write()         { // 统计写入(读取的行数)             int WriteRows = 0;

// 读取文件的源路径及其读取流             string strReadFilePath = @"../../data/ReadLog.txt";             StreamReader srReadFile = new StreamReader(strReadFilePath);

  // 写入文件的源路径及其写入流             string strWriteFilePath = @"../../data/WriteLog.txt";             StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

  // 读取流直至文件末尾结束,并逐行写入另一文件内             while (!srReadFile.EndOfStream)             {                 string strReadLine = srReadFile.ReadLine(); //读取每行数据                 ++WriteRows; //统计写入(读取)的数据行数

                swWriteFile.WriteLine(strReadLine); //写入读取的每行数据                 Console.WriteLine("正在写入... " + strReadLine);             }

// 关闭流文件             srReadFile.Close();             swWriteFile.Close();

            Console.WriteLine("共计写入记录总数:" + WriteRows);             Console.ReadKey();         }

========================================================================

完整源代码(经过本人测试,直接运行就可)

using System; using System.Collections.Generic; using System.Linq; using System.Text;

using System.IO; // 引用输入输出操作的命令空间

namespace ReadWriteFile {     class Program     {

// 主函数         static void Main(string[] args)         {             Read(); // 读操作

            Write(); // 写操作         }

// 读操作         public static void Read()         {             // 读取文件的源路径及其读取流             string strReadFilePath = @"../../data/ReadLog.txt";             StreamReader srReadFile = new StreamReader(strReadFilePath);

            // 读取流直至文件末尾结束             while (!srReadFile.EndOfStream)             {                 string strReadLine = srReadFile.ReadLine(); //读取每行数据                 Console.WriteLine(strReadLine); //屏幕打印每行数据             }

            // 关闭读取流文件             srReadFile.Close();             Console.ReadKey();         }

// 写操作         public static void Write()         {             // 统计写入(读取的行数)             int WriteRows = 0;

            // 读取文件的源路径及其读取流             string strReadFilePath = @"../../data/ReadLog.txt";             StreamReader srReadFile = new StreamReader(strReadFilePath);

            // 写入文件的源路径及其写入流             string strWriteFilePath = @"../../data/WriteLog.txt";             StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

            // 读取流直至文件末尾结束,并逐行写入另一文件内             while (!srReadFile.EndOfStream)             {                 string strReadLine = srReadFile.ReadLine(); //读取每行数据                 ++WriteRows; //统计写入(读取)的数据行数

                swWriteFile.WriteLine(strReadLine); //写入读取的每行数据                 Console.WriteLine("正在写入... " + strReadLine);             }

            // 关闭流文件             srReadFile.Close();             swWriteFile.Close();

            Console.WriteLine("共计写入记录总数:" + WriteRows);             Console.ReadKey();         }     } }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009年12月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C#读取文本文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档