2025-01-02 12:15:45 +08:00

117 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
/*******************************************************************************
*Create By CG
*Function txt文件操作
*******************************************************************************/
namespace CG.UTility
{
public static class TxtFileHandle
{
/// <summary>
/// 给txt文件添加内容,如果没有文件无法写入
/// </summary>
/// <param name="txtFilePath">文件路径</param>
/// <param name="contents">需要添加的内容</param>
/// <param name="replace">是否替换原有文件</param>
public static void WriteAllTxt(string txtFilePath, string contents,bool replace)
{
if (!File.Exists(txtFilePath)) return;
if (replace)
{
File.WriteAllText(txtFilePath, contents);
}
else
{
using (StreamWriter file = new StreamWriter(txtFilePath, true))
{
file.WriteLine(contents);
}
}
}
/// <summary>
/// 给txt文件添加多条内容,如果没有文件则创建文件写入
/// </summary>
/// <param name="txtFilePath">文件路径</param>
/// <param name="contents">需要添加的内容</param>
/// <param name="replace">是否替换原有文件</param>
public static void WriteAllTxt(string txtFilePath, string[] contents, bool replace)
{
try
{
using (StreamWriter file = new StreamWriter(txtFilePath, !replace))
{
for (int i = 0; i < contents.Length; i++)
{
file.WriteLine(contents[i]);
}
}
}
catch (System.Exception ex)
{
WDebug.LogError("WriteAllTxt Erro :" + ex.Message);
return;
}
}
/// <summary>
/// 读取txt文件中所有内容适宜小数据量
/// </summary>
/// <param name="txtFilePath">txt文件路径</param>
/// <returns>读取到的所有内容</returns>
public static void ReadAllTxt(string txtFilePath, System.Action<string> actionResult)
{
//return File.ReadAllText(txtFilePath);
MonoManager.Instance.StartCoroutine(GetTextAsyn(txtFilePath, actionResult));
}
private static IEnumerator GetTextAsyn(string url, System.Action<string> actionResult)
{
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
string t = request.downloadHandler.text;
if (string.IsNullOrEmpty(t)) WDebug.LogError("GetTextAsyn()/ Get Text is error! url:" + url);
actionResult?.Invoke(t);
}
/// <summary>
/// 读取txt文件中所有内容
/// </summary>
/// <param name="txtFilePath">txt文件路径</param>
/// <returns>读取到的所有行列内容</returns>
public static string[] ReadAllLines(string txtFilePath)
{
WDebug.Log("Path:" + txtFilePath);
return File.ReadAllLines(txtFilePath);
}
/// <summary>
/// 读取txt文件中所有内容适宜大数据量
/// </summary>
/// <param name="txtFilePath">txt文件路径</param>
/// <returns>读取到的所有行列内容</returns>
public static string[] ReadBigTxt(string txtFilePath)
{
List<string> list = new List<string>();
try
{
using (StreamReader sr = new StreamReader(txtFilePath))
{
while (!sr.EndOfStream)
{
string readData = sr.ReadLine();
list.Add(readData);
}
}
}
catch (System.Exception ex)
{
WDebug.LogError("ReadBigTxt Erro :" + ex.Message);
return null;
}
return list.ToArray();
}
}
}