亚洲乱色熟女一区二区三区丝袜,天堂√中文最新版在线,亚洲精品乱码久久久久久蜜桃图片,香蕉久久久久久av成人,欧美丰满熟妇bbb久久久

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# Txt文本文件讀、寫處理函數(shù)

admin
2025年7月20日 19:28 本文熱度 828

      TXT(純文本)文件是最基礎(chǔ)、最通用的文件格式之一,在編程和系統(tǒng)管理中廣泛應(yīng)用。它不包含任何格式(如字體、顏色等),僅存儲(chǔ)純文本數(shù)據(jù),具有極高的兼容性和靈活性。

      在系統(tǒng)/應(yīng)用程序中常常使用txt文檔保存日志記錄,例如:Web服務(wù)器日志、數(shù)據(jù)庫(kù)查詢?nèi)罩尽?/span>應(yīng)用程序調(diào)試日志。

  • 優(yōu)點(diǎn):

     跨平臺(tái)兼容:所有操作系統(tǒng)和編程語(yǔ)言原生支持。

     輕量高效:無(wú)格式開銷,讀寫速度快。

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

      可讀性強(qiáng):人類可直接閱讀和修改。

  • 缺點(diǎn):

     無(wú)結(jié)構(gòu)化支持:需自行解析(如按行、按分隔符拆分)。

      無(wú)元數(shù)據(jù):無(wú)法存儲(chǔ)編碼、創(chuàng)建時(shí)間等額外信息。

      安全性低:明文存儲(chǔ)敏感數(shù)據(jù)需額外加密。

調(diào)用方法如下:

1、寫方法調(diào)用函數(shù):
1)文件流方式追加寫入
/// <summary>///  寫Txt文本內(nèi)容(文件流方式追加寫入)/// </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)內(nèi)置函數(shù)追加寫入
/// <summary>  /// 寫Txt文本內(nèi)容(內(nèi)置函數(shù)追加寫入)/// </summary>  /// <param name="filePath">文件的絕對(duì)路徑</param>  /// <param name="content">寫入的內(nèi)容</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文本內(nèi)容(文本流方式覆蓋寫入)/// </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)內(nèi)置函數(shù)覆蓋寫入
/// <summary>  /// 寫Txt文本內(nèi)容(內(nèi)置函數(shù)覆蓋寫入)  /// </summary>  /// <param name="filePath">文件的絕對(duì)路徑</param>  /// <param name="content">寫入的內(nèi)容</param>          public void CreateText(string filePath, string content){    //向文件寫入內(nèi)容      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、讀取方法調(diào)用函數(shù):

1)、按文件流程方式讀取
/// <summary>/// 獲取Txt內(nèi)容列表(按文件流程方式)/// </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"用于顯示中文字符,寫其他的,中文會(huì)顯示亂碼        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)、按內(nèi)置函數(shù)方法讀取
public List<stringReadTxtStrList(string TxtFilePath){
    string[] RetLine = File.ReadAllLines(TxtFilePath);//按行獲得,返回?cái)?shù)組集合;
    return RetLine.ToList<string>();//將數(shù)組轉(zhuǎn)換為列表處理;//return File.ReadAllText(TxtFilePath);//讀取Txt文本中的所有內(nèi)容,返回一個(gè)長(zhǎng)的字符串
}


閱讀原文:原文鏈接


該文章在 2025/7/21 10:30:55 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved