【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

相关推荐
yuanpan1 小时前
.net/C#进程间通信技术方案总结
开发语言·c#·.net
IM1GENIUS3 小时前
.NET高频技术点(持续更新中)
c#·.net
red-fly4 小时前
c#修改ComboBox当前选中项的文本
c#·combobox
bicijinlian7 小时前
.Net HttpClient 概述
c#·.net·httpclient·.net httpclient
码观天工8 小时前
.NET 原生驾驭 AI 新基建实战系列(七):Weaviate ── 语义搜索的智能引擎创新者
ai·c#·.net·向量数据库·weaviate
Zhen (Evan) Wang8 小时前
.NET 8 + Angular WebSocket 高并发性能优化
c#·.net·angular
chenyuhao20249 小时前
链表面试题7之相交链表
数据结构·算法·链表·面试·c#
菜鸟分享录10 小时前
MCP 入门实战:用 C# 开启 AI 新篇章
ai·c#·semantic kernel·mcp
编程乐趣11 小时前
一个用C#开发的记事本Notepads开源编辑器
c#·编辑器·.net
XYR1212121 天前
C# 参数
c#