【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

相关推荐
软件黑马王子2 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
水煮庄周鱼鱼7 小时前
C# 入门简介
开发语言·c#
软件黑马王子8 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
Nicole Potter8 小时前
请说明C#中的List是如何扩容的?
开发语言·面试·c#
gu2010 小时前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
pchmi14 小时前
CNN常用卷积核
深度学习·神经网络·机器学习·cnn·c#
yuanpan15 小时前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式
滴_咕噜咕噜16 小时前
C#基础总结:常用的数据结构
开发语言·数据结构·c#
万兴丶19 小时前
Unity 适用于单机游戏的红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含源码)
数据结构·unity·设计模式·c#
程序猿多布20 小时前
C#设计模式 学习笔记
设计模式·c#