C#读取和写入txt文档(在unity中示例)

本篇内容简单介绍如何在c#中内容读取和写入txt文档

注意:先在Unity的StreamingAssets文件夹中创建一个txt文档

一、读取txt

1.1全部一起读取

cs 复制代码
private void ReadText01()
{
    string filePath = Path.Combine(Application.streamingAssetsPath, "testTXT.txt");
    // 读取文件内容
    if (File.Exists(filePath))
    {
        string fileContent = File.ReadAllText(filePath);
        Debug.Log(fileContent); // 输出文件内容
    }
    else
    {
        Debug.LogError("文件不存在: " + filePath);
    }
}

1.2全部逐行读取

cs 复制代码
private void ReadText02()//逐行读取
{
    string filePath2 = Path.Combine(Application.streamingAssetsPath, "testTXT.txt");
    // 读取文件内容
    if (File.Exists(filePath2))
    {
        using (StreamReader reader = new StreamReader(filePath2))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                Debug.Log(line); // 输出每一行内容
            }
        }
    }
    else
    {
        Debug.LogError("文件不存在: " + filePath2);
    }
}

二、写入txt

2.1全部一起写入(删除旧内容,添加新内容)

cs 复制代码
 private void WriteTxt01()//全部写入
 {
     string filePath3 = Path.Combine(Application.streamingAssetsPath, "testTXT.txt");
     // 要写入的内容
     string contentToWrite = "方法1小文件写入txt";
     // 写入文件内容
     File.WriteAllText(filePath3, contentToWrite);
 }

2.2全部逐行写入(删除旧内容,添加新内容)

cs 复制代码
private void WriteTxt02()//逐行写入
{
    string filePath4 = Path.Combine(Application.streamingAssetsPath, "testTXT.txt");
    // 要写入的内容
    string contentToWrite2 = "Hello, this is a test message.\nThis is a new line.";
    // 使用 StreamWriter 写入文件内容
    using (StreamWriter writer = new StreamWriter(filePath4))
    {
        writer.WriteLine(contentToWrite2); // 写入内容
    }
}

2.3全部一起写入(不删除旧内容情况下直接添加新内容)

cs 复制代码
private void WriteTxt01()//全部写入
{
    string filePath3 = Path.Combine(Application.streamingAssetsPath, "testTXT.txt");
    // 要写入的内容
    string contentToWrite = "方法1小文件写入txt";
    // 追加文件内容
    File.AppendAllText(filePath3, contentToWrite);
}

2.4全部逐行写入(不删除旧内容情况下直接添加新内容)

cs 复制代码
private void WriteTxt02()//支持逐行写入
{
    // 设置文本文件的路径(在 Unity 的 StreamingAssets 文件夹中)
    string filePath4 = Path.Combine(Application.streamingAssetsPath, "testTXT.txt");

    // 要写入的内容
    string contentToWrite2 = "Hello, this is a test message.\nThis is a new line.";
    using (StreamWriter writer2 = new StreamWriter(filePath4, true))
    {
        writer2.WriteLine("This line will be appended.");
    }
}
相关推荐
Clank的游戏栈2 小时前
Unity多线程渲染指令队列设计与集成技术详解
windows·unity·游戏引擎
程序设计实验室5 小时前
一次小而美的重构:使用 C# 在 Avalonia 中生成真正好看的词云
c#
电商api接口开发6 小时前
ASP.NET MVC 入门指南二
前端·c#·html·mvc
Thomas_YXQ9 小时前
Unity3D IK解算器技术分析
开发语言·搜索引擎·unity·全文检索·unity3d·lucene
o0向阳而生0o9 小时前
28、.NET 中元数据是什么?
microsoft·c#·.net
niuTaylor10 小时前
Linux驱动开发快速上手指南:从理论到实战
linux·运维·开发语言·驱动开发·c#
军训猫猫头10 小时前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
冰茶_12 小时前
.NET MAUI 发展历程:从 Xamarin 到现代跨平台应用开发框架
学习·microsoft·微软·c#·.net·xamarin
The Future is mine13 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
星火撩猿17 小时前
常见游戏引擎介绍与对比
unity·ue5·游戏引擎·godot