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#程序中對(duì)MessageBox進(jìn)行定位的方法
這篇文章主要介紹了在C#程序中對(duì)MessageBox進(jìn)行定位的方法,針對(duì)圖形化界面進(jìn)行調(diào)試,需要的朋友可以參考下2015-07-07
C# char類型字符轉(zhuǎn)換大小寫的實(shí)現(xiàn)代碼
以下是對(duì)C#中char類型字符轉(zhuǎn)換大小寫的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下哦2013-07-07
C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引
這篇文章主要介紹了C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引的相關(guān)資料,需要的朋友可以參考下2018-01-01
C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
C#實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫入數(shù)據(jù)庫和DataTable
Excel文件是存儲(chǔ)表格數(shù)據(jù)的普遍格式,因此能夠高效地讀取和提取信息對(duì)于我們來說至關(guān)重要,下面我們就來看看C#如何實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫入數(shù)據(jù)庫和DataTable吧2024-03-03

