【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

相关推荐
绿荫阿广6 小时前
互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(五):使用.NET为树莓派开发Wifi配网功能
c#·.net
ou.cs8 小时前
c# :this() 和 :base()区别
开发语言·c#
汪小白JIY11 小时前
【C#】异步和多线程
c#·thread·async·task·threapool
宝桥南山13 小时前
DeepSeek - 尝试一下GitHub Models中的DeepSeek
microsoft·ai·微软·c#·github·.net
lljss202019 小时前
C# 一个解决方案放一个dll项目,一个dll测试项目 ,调试dll项目的源码
c#
ghost1431 天前
C#学习第27天:时间和日期的处理
开发语言·学习·c#
jason成都1 天前
c#压缩与解压缩-SharpCompress
开发语言·c#
傻啦嘿哟1 天前
从零开始:用Tkinter打造你的第一个Python桌面应用
开发语言·c#
CodeCraft Studio1 天前
PDF处理控件Aspose.PDF教程:在 C# 中更改 PDF 页面大小
前端·pdf·c#
InCerry1 天前
.NET周刊【5月第4期 2025-05-25】
c#·.net·.net周刊