【C#】读取与写入txt文件内容

在 C# 中读取和写入文本文件内容是一个常见的任务。以下是使用几种不同方法读取和写入文本文件的示例。

一、读取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 (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)
        {
            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(filePath);

            // 打印文件内容
            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(filePath, 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.WriteAllTextFile.AppendAllText方法会创建一个新文件,并分别覆盖或追加内容。如果文件已经存在,它们会相应地写入或追加内容。

需要注意的是,这些方法会在没有提示的情况下覆盖现有文件的内容(File.WriteAllText),所以在使用时要小心,确保你了解这些操作的影响。

 

三、C++ 写入txt文件内容并追加内容

3.1 C++ 写入txt文件内容并追加内容 

C++ 写入txt文件内容并追加内容_qt ofstream写数据到txt-CSDN博客文章浏览阅读2.9k次,点赞3次,收藏9次。C++ 写入txt文件内容并追加内容_qt ofstream写数据到txthttps://blog.csdn.net/wangnaisheng/article/details/132410040

 

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangnaisheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值