C#讀取與寫入txt文件內(nèi)容的實(shí)現(xiàn)方法
一、讀取txt文件內(nèi)容
1.1 使用 StreamReader
using System; using System.IO; class Program { static void Main() { string filePath = @"C:\path\to\your\file.txt"; try { using (StreamReader reader = new StreamReader(filePath)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } catch (FileNotFoundException) { Console.WriteLine("文件未找到,請(qǐng)檢查文件路徑是否正確。"); } catch (IOException e) { Console.WriteLine("讀取文件時(shí)發(fā)生錯(cuò)誤: " + e.Message); } } }
1.2 使用 File.ReadAllLines
using System; using System.IO; class Program { static void Main() { string filePath = @"C:\path\to\your\file.txt"; try { string[] lines = File.ReadAllLines(filePath); foreach (string line in lines) { Console.WriteLine(line); } } catch (FileNotFoundException) { Console.WriteLine("文件未找到,請(qǐng)檢查文件路徑是否正確。"); } catch (IOException e) { Console.WriteLine("讀取文件時(shí)發(fā)生錯(cuò)誤: " + e.Message); } } }
1.3 使用 File.ReadAllText
using System; using System.IO; class Program { static void Main() { string filePath = @"C:\path\to\your\file.txt"; try { // 讀取整個(gè)文件到一個(gè)字符串變量 string content = File.ReadAllText(filePath); // 打印文件內(nèi)容 Console.WriteLine(content); } catch (FileNotFoundException) { Console.WriteLine("文件未找到,請(qǐng)檢查文件路徑是否正確。"); } catch (IOException e) { Console.WriteLine("讀取文件時(shí)發(fā)生錯(cuò)誤: " + e.Message); } } }
1.4 注意點(diǎn)
在寫入文件之前,務(wù)必檢查文件和目錄是否存在,以避免不必要的錯(cuò)誤。使用 try-catch
塊來捕獲并處理任何可能發(fā)生的異常,這是一個(gè)良好的編程實(shí)踐。
二、寫入txt文件內(nèi)容
2.1 追加內(nèi)容到文本文件
using System; using System.IO; class Program { static void Main() { string filePath = @"C:\path\to\your\file.txt"; string contentToAppend = "新添加的一行內(nèi)容。\n"; try { File.AppendAllText(filePath, contentToAppend); Console.WriteLine("內(nèi)容已追加到文件。"); } catch (IOException e) { Console.WriteLine("寫入文件時(shí)發(fā)生錯(cuò)誤: " + e.Message); } } }
2.2 覆蓋內(nèi)容到文本文件
using System; using System.IO; class Program { static void Main() { string filePath = @"C:\path\to\your\file.txt"; string contentToWrite = "這是新的文件內(nèi)容。\n"; try { File.WriteAllText(filePath, contentToWrite); Console.WriteLine("文件內(nèi)容已更新。"); } catch (IOException e) { Console.WriteLine("寫入文件時(shí)發(fā)生錯(cuò)誤: " + e.Message); } } }
2.3 注意點(diǎn)
在上述兩個(gè)示例中,如果指定的文件路徑不存在,F(xiàn)ile.WriteAllText和File.AppendAllText方法會(huì)創(chuàng)建一個(gè)新文件,并分別覆蓋或追加內(nèi)容。如果文件已經(jīng)存在,它們會(huì)相應(yīng)地寫入或追加內(nèi)容。
需要注意的是,這些方法會(huì)在沒有提示的情況下覆蓋現(xiàn)有文件的內(nèi)容(File.WriteAllText),所以在使用時(shí)要小心,確保你了解這些操作的影響。
三、C++ 寫入txt文件內(nèi)容并追加內(nèi)容
可以使用ofstream類來寫入txt文件內(nèi)容。若想追加內(nèi)容,可以使用ios::app標(biāo)志來創(chuàng)建輸出流對(duì)象,然后在寫入時(shí)將其設(shè)置為ios::app。以下是一個(gè)示例代碼:
#include <iostream> #include <fstream> using namespace std; int main() { ofstream out(""D:\\example.txt", ios::app); time_t now = time(nullptr); char timeStr[30]; strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localtime(&now)); out << timeStr << endl; out << "Hello, World!" << endl; out << "This is an example." << endl; out.close(); return 0; }
在這個(gè)例子中,我們創(chuàng)建了一個(gè)名為“example.txt”的輸出流對(duì)象,并將其設(shè)置為ios::app。然后,我們寫入了兩行文本,并在文件末尾添加了它們。最后,我們關(guān)閉了輸出流對(duì)象。
若想在文件開頭追加內(nèi)容,可以使用ios::ate標(biāo)志來創(chuàng)建輸出流對(duì)象。這將導(dǎo)致輸出流對(duì)象自動(dòng)跳過文件開頭的內(nèi)容,并將下一個(gè)寫入操作添加到文件末尾。以下是一個(gè)示例代碼:
#include <iostream> #include <fstream> using namespace std; int main() { ofstream out(""D:\\example.txt", ios::ate | ios::out); time_t now = time(nullptr); char timeStr[30]; strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localtime(&now)); out << timeStr << endl; out << "This is an example." << endl; out << "Hello, World!" << endl; out.close(); return 0; }
在這個(gè)例子中,我們創(chuàng)建了一個(gè)名為“example.txt”的輸出流對(duì)象,并將其設(shè)置為ios::ate | ios::out。然后,我們寫入了兩行文本,并在文件末尾添加了它們。最后,我們關(guān)閉了輸出流對(duì)象。
以上就是C#讀取與寫入txt文件內(nèi)容的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于C#讀取與寫入txt內(nèi)容的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# 中的 is 真的是越來越強(qiáng)大越來越語義化(推薦)
這篇文章主要介紹了C# 中的 is 真的是越來越強(qiáng)大越來越語義化,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法
本文主要介紹了C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
這篇文章主要介紹了unity 文件流讀取圖片與www讀取圖片的對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#窗體實(shí)現(xiàn)點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#窗體實(shí)現(xiàn)點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C#對(duì)Task中的異常進(jìn)行捕獲的幾種常見方法
在C#中異步Task是一個(gè)很方便的語法,經(jīng)常用在處理異步,例如需要下載等待等方法中,不用函數(shù)跳轉(zhuǎn),代碼閱讀性大大提高,深受大家喜歡,但是有時(shí)候發(fā)現(xiàn)我們的異步函數(shù)可能出現(xiàn)了報(bào)錯(cuò),本文給大家介紹了C#對(duì)Task中的異常進(jìn)行捕獲的幾種常見方法,需要的朋友可以參考下2025-01-01解析C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用,備忘錄模式用來保存與對(duì)象有關(guān)的數(shù)據(jù)用以在將來對(duì)對(duì)象進(jìn)行復(fù)原,需要的朋友可以參考下2016-02-02C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
這篇文章繼續(xù)介紹了C#數(shù)據(jù)類型和變量,是對(duì)上一篇文章的補(bǔ)充,希望對(duì)大家的學(xué)習(xí)有所幫助。2015-10-10