C#讀取與寫入txt文件內(nèi)容的實現(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("文件未找到,請檢查文件路徑是否正確。"); } catch (IOException e) { Console.WriteLine("讀取文件時發(fā)生錯誤: " + 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("文件未找到,請檢查文件路徑是否正確。"); } catch (IOException e) { Console.WriteLine("讀取文件時發(fā)生錯誤: " + 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 { // 讀取整個文件到一個字符串變量 string content = File.ReadAllText(filePath); // 打印文件內(nèi)容 Console.WriteLine(content); } catch (FileNotFoundException) { Console.WriteLine("文件未找到,請檢查文件路徑是否正確。"); } catch (IOException e) { Console.WriteLine("讀取文件時發(fā)生錯誤: " + e.Message); } } }
1.4 注意點
在寫入文件之前,務必檢查文件和目錄是否存在,以避免不必要的錯誤。使用 try-catch
塊來捕獲并處理任何可能發(fā)生的異常,這是一個良好的編程實踐。
二、寫入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("寫入文件時發(fā)生錯誤: " + 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("寫入文件時發(fā)生錯誤: " + e.Message); } } }
2.3 注意點
在上述兩個示例中,如果指定的文件路徑不存在,F(xiàn)ile.WriteAllText和File.AppendAllText方法會創(chuàng)建一個新文件,并分別覆蓋或追加內(nèi)容。如果文件已經(jīng)存在,它們會相應地寫入或追加內(nèi)容。
需要注意的是,這些方法會在沒有提示的情況下覆蓋現(xiàn)有文件的內(nèi)容(File.WriteAllText),所以在使用時要小心,確保你了解這些操作的影響。
三、C++ 寫入txt文件內(nèi)容并追加內(nèi)容
可以使用ofstream類來寫入txt文件內(nèi)容。若想追加內(nèi)容,可以使用ios::app標志來創(chuàng)建輸出流對象,然后在寫入時將其設置為ios::app。以下是一個示例代碼:
#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; }
在這個例子中,我們創(chuàng)建了一個名為“example.txt”的輸出流對象,并將其設置為ios::app。然后,我們寫入了兩行文本,并在文件末尾添加了它們。最后,我們關閉了輸出流對象。
若想在文件開頭追加內(nèi)容,可以使用ios::ate標志來創(chuàng)建輸出流對象。這將導致輸出流對象自動跳過文件開頭的內(nèi)容,并將下一個寫入操作添加到文件末尾。以下是一個示例代碼:
#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; }
在這個例子中,我們創(chuàng)建了一個名為“example.txt”的輸出流對象,并將其設置為ios::ate | ios::out。然后,我們寫入了兩行文本,并在文件末尾添加了它們。最后,我們關閉了輸出流對象。
以上就是C#讀取與寫入txt文件內(nèi)容的實現(xiàn)方法的詳細內(nèi)容,更多關于C#讀取與寫入txt內(nèi)容的資料請關注腳本之家其它相關文章!
相關文章
C#中通過反射將枚舉元素加載到ComboBo的實現(xiàn)方法
本文主要介紹了C#中通過反射將枚舉元素加載到ComboBo的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
這篇文章主要介紹了unity 文件流讀取圖片與www讀取圖片的對比分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04