C# 读写TXT文件操作

一 、读取文本文件有多种方法

1 使用 StreamReader 类打开、读取和关闭文本文件。使用 ReadLine 方法逐行读取文本文件,ReadToEnd方法将文本文件读取到末尾。

        /// <summary>
        /// 逐行将文本文件读取到末尾
        /// </summary>
        /// <param name="pathAName"></param>
        /// <returns></returns>
        public static string ReadTXT_StreamReader(string pathAName)
        {
   
   
            StreamReader ObjectName = new StreamReader(pathAName);
            return ObjectName.ReadToEnd(); 
        }

2、StreamReader继承TextReader,操作和StreamReader一致,使用 ReadLine 方法逐行读取文本文件,ReadToEnd方法将文本文件读取到末尾。

        /// <summary>
        /// 逐行读取文件,返回列表
        /// </summary>
        /// <param name="pathAName"></param>
        /// <returns></returns>
        public static List<string> ReadTXT_TextReader(string pathAName)
        {
   
   
            List<string> txtContent = new List<string>();
            if (!File.Exists(pathAName))
            {
   
   
                using (TextReader reader = new StreamReader(pathAName))
                {
   
   
                    string line;
                    while ((line = reader.ReadLine()) != null) // 逐行读取文件内容
                    {
   
   
                        txtContent.Add(line); // 输出每行内容到控制台
                    }
                }
            }
            return txtContent;
        }

3、使用 File 类打开、读取和关闭文本文件。
File.ReadAllLines(string path, Encoding encoding) 方法:打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。返回包含文件所有行的字符串数组。

        /// <summary>
        /// 逐行读取文件,返回数组
        /// </summary>
        /// <param name="pathAName"></param>
        /// <returns></returns>
        public static string[] ReadTXT_ReadAllLines(string pathAName)
        {
   
   
            string[] txtContent = null;
            txtContent = System.IO.File.ReadAllLines(pathAName, Encoding.UTF8);
            return txtContent;
        }

File.ReadAllText(string path, Encoding encoding)方法:打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。逐行读取文本文件。返回包含文件所有行的字符串。

        /// <summary>
        /// 读取,返回字符串
        /// </summary>
        /// <param name="pathAName"></param>
        /// <returns></returns>
        public static string ReadTXT_ReadAllText(string pathAName)
        {
   
   
            string txtContent = null;
            txtContent = System.IO.File.ReadAllText(pathAName, Encoding.UTF8);
            return txtContent;
        }

ReadLines(string path, Encoding encoding)方法:读取具有指定编码的文件的文本行。返回该文件的所有行或查询所示的行。

        /// <summary>
        /// 读取,返回枚举类型
        /// 高效地逐行读取大型文本文件的最佳方法
        /// </summary>
        /// <param name="pathAName"></param>
        /// <returns></returns>
        public static string ReadTXT_ReadLines(string pathAName)
        {
   
   
            IEnumerable<string> line = System.IO.File.ReadLines(pathAName, Encoding.UTF8);
            return String.Join(Environment.NewLine, line);
        }

二、创建、写入文本文件

1、FileStream 创建文件,可设置文件属性,StreamWriter 打开、写入、关闭文件。
a)方式1:

        /// <summary>
        /// 数据流形式写入文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="txtContent"></param>
        /// <returns></returns>
        public static void WriteTXT_StreamWriter(string pathAName, string txtContent)
        {
   
   
            FileStream fs;
            //判断是否已经有了这个文件
            if (!System.IO.File.Exists(pathAName))
            {
   
   
                //没有则创建这个文件,创建写入文件,设置文件属性为隐藏
                fs = new FileStream(pathAName, FileMode.Create, FileAccess.Write);
            }
            else
            {
   
   
                fs = new FileStream(pathAName, FileMode.Open, FileAccess.Write);
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值