LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# Txt文本文件读、写处理函数

admin
2025年7月20日 19:28 本文热度 70

      TXT(纯文本)文件是最基础、最通用的文件格式之一,在编程和系统管理中广泛应用。它不包含任何格式(如字体、颜色等),仅存储纯文本数据,具有极高的兼容性和灵活性。

      在系统/应用程序中常常使用txt文档保存日志记录,例如:Web服务器日志、数据库查询日志、应用程序调试日志。

  • 优点:

     跨平台兼容:所有操作系统和编程语言原生支持。

     轻量高效:无格式开销,读写速度快。

     易于处理:可用任何文本编辑器或命令行工具(如cat、grep)操作。

      可读性强:人类可直接阅读和修改。

  • 缺点:

     无结构化支持:需自行解析(如按行、按分隔符拆分)。

      无元数据:无法存储编码、创建时间等额外信息。

      安全性低:明文存储敏感数据需额外加密。

调用方法如下:

1、写方法调用函数:
1)文件流方式追加写入
/// <summary>///  写Txt文本内容(文件流方式追加写入)/// </summary>/// <param name="path"></param>/// <param name="log"></param>/// <param name="IsEnterLine"></param>public void AppendWritTxt(string log, string path, bool IsEnterLine = false){    try    {        using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))        {            using (StreamWriter sw = new StreamWriter(fs))            {                if (IsEnterLine)                    sw.WriteLine(log.ToString());                else                    sw.Write(log.ToString());                sw.Flush();            }        }    }    catch (Exception e)    {        string err = e.Message;    } }
2)内置函数追加写入
/// <summary>  /// 写Txt文本内容(内置函数追加写入)/// </summary>  /// <param name="filePath">文件的绝对路径</param>  /// <param name="content">写入的内容</param>  public void AppendText(string filePath, string content){    File.AppendAllText(filePath, content);}
///public void AppendText(string filePath, List<string> strList){    File.AppendAllLines(filePath, strList);    //File.AppendAllLines(filePath, strList, Encoding.UTF8);}
3)文本流方式覆盖写入
/// <summary>/// 写Txt文本内容(文本流方式覆盖写入)/// </summary>/// <param name="TxtStr"></param>/// <param name="path"></param>public void CreateWritTxt(string path, string TxtStr, bool IsEnterLine = false){    try    {        using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))        {            using (StreamWriter sw = new StreamWriter(fs))            {                if (IsEnterLine)                    sw.WriteLine(TxtStr.ToString());                else                    sw.Write(TxtStr.ToString());                sw.Flush();            }        }    }    catch (Exception e)    {        string err = e.Message;    }        }
4)内置函数覆盖写入
/// <summary>  /// 写Txt文本内容(内置函数覆盖写入)  /// </summary>  /// <param name="filePath">文件的绝对路径</param>  /// <param name="content">写入的内容</param>          public void CreateText(string filePath, string content){    //向文件写入内容      File.WriteAllText(filePath, content);}public void CreateText(string filePath, List<string> strList){    File.WriteAllLines(filePath, strList);}public void CreateText(string filePath, string[] str){    File.WriteAllLines(filePath, str);}public void CreateText(string filePath, byte[] str){    File.WriteAllBytes(filePath, str);}
2、读取方法调用函数:

1)、按文件流程方式读取
/// <summary>/// 获取Txt内容列表(按文件流程方式)/// </summary>/// <returns></returns>public List<stringReadTxtList(string FilePath){    List<string> RetList = new List<string>();    if (!File.Exists(FilePath)) return null;
    string ReatStr = string.Empty;    using (FileStream fs = new FileStream(FilePath, FileMode.Open))    {        // "GB2312"用于显示中文字符,写其他的,中文会显示乱码        using (StreamReader reader = new StreamReader(fs, UnicodeEncoding.GetEncoding("GB2312")))//Encoding.Default  Encoding.UTF8        {            //一行一行读取            while ((ReatStr = reader.ReadLine()) != null)            {                ReatStr = ReatStr.Trim().ToString();                RetList.Add(ReatStr);            }            reader.Dispose();            fs.Dispose();        }    }    return RetList;}
2)、按内置函数方法读取
public List<stringReadTxtStrList(string TxtFilePath){
    string[] RetLine = File.ReadAllLines(TxtFilePath);//按行获得,返回数组集合;
    return RetLine.ToList<string>();//将数组转换为列表处理;//return File.ReadAllText(TxtFilePath);//读取Txt文本中的所有内容,返回一个长的字符串
}


阅读原文:原文链接


该文章在 2025/7/21 10:30:55 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved