目录
- 一、读取txt文件内容
- 1.1 使用 StreamReader
- 1.2 使用 File.ReadAllLines
- 1.3 使用 File.ReadAllText
- 1.4 注意点
- 二、写入txt文件内容
- 2.1 追加内容到文本文件
- 2.2 覆盖内容到文本文件
- 2.3 注意点
- 三、C++ 写入txt文件内容并追加内容
一、读取txt文件内容
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 android(IOException e)
{
Console.WriteLine("读取文件时发生错误: " + 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)
{
android Console.WriteLine("读取文件时发生错误: " + 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(filePajavascriptth);
// 打印文件内容
Console.WriteLine(content);
}
catch (FileNotFoundException)
{
Console.WriteLine("文件未找到,请检查文件路径是否正确。");
}
catch (IOException e)
{
Console.WriteLine("读取文件时发生错误: " + e.Message);
}
}
}
1.4 注意点
在写入文件之前,务必检查文件和目录是否存在,以避免不必要的错误。使用 try-catch 块来捕获并处理任何可能发生的异常,这是一个良好的编程实践。
二、写入txt文件内容
2.1 追加内容到文本文件
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"C:\path\to\your\file.txt";
string contentToAppend = "新添加的一行内容。\n";
try
{
File.AppendAllText(fileP编程客栈ath, contentToAppend);
Console.WriteLine("内容已追加到文件。");
}
catch (IOException e)
{
Console.WriteLine("写入文件时发生错误: " + e.Message);
}
}
}
2.2 覆盖内容到文本文件
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"C:\path\to\your\file.txt";
string contentToWrite = "这是新的文件内容。\n";
try
{
File.WriteAllText(filePath, contentToWrite);
Console.WriteLine("文件内容已更新。");
}
catch (IOException e)
{
Console.WriteLine("写入文件时发生错误: " + e.Message);
}
}
}
2.3 注意点
在上述两个示例中,如果指定的文件路径不存在,File.WriteAllText和File.AppendAllText方法会创建一个新文件,并分别覆盖或追加内容。如果文件已经存在,它们会相应地写入或追加内容。
需要注意的是,这些方法会在没有提示的情况下覆盖现有文件的内容(File.WriteAllText),所以在使用时要小心,确保你了解这些操作的影响。
三、C++ 写入txt文件内容并追加内容
可以使用ofstream类来写入txt文件内容。若想追加内容,可以使用IOS::app标志来创建输出流对象,然后在写入时将其设置为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;
}
在这个例子中,我们创建了一个名为“example.txt”的输出流对象,并将其设置为ios::app。然后,我们写入了两行文本,并在文件末尾添加了它们。最后,我们关闭了输出流对象。
若想在文件开头追加内容,可以使用ios::ate标志来创建输出流对象。这将导致输出流对象自动跳过文件开头的内容,并将下一个写入操作添加到文件末尾。以下是一个示例代码:
#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;
}
在这个例子中,我们创建了一个名为“example.txt”的输出流对象,并将其设置为ios::ate | ios::out。然后,我们写入了两行文本,并在文件末尾添加了它们。最后,我们关闭了输出流对象。
以上就是C#读取与写入txt文件内容的实现方法的详细内容,更多关于C#读取与写入txt内容的资料请关注编程客栈(www.devze.com)其它相关文章!
加载中,请稍侯......
精彩评论