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

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

一、读取txt文件内容

1.1 使用 StreamReader

cs 复制代码
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

cs 复制代码
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

cs 复制代码
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 追加内容到文本文件

cs 复制代码
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 覆盖内容到文本文件

cs 复制代码
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

相关推荐
c#上位机1 小时前
C#事件的用法
java·javascript·c#
chnyi6_ya1 小时前
一些写leetcode的笔记
笔记·leetcode·c#
IT规划师1 小时前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
时光追逐者2 小时前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
friklogff2 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
__water11 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water11 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water12 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
__water14 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。15 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎