2025-03-10 10:18:11 +08:00

134 lines
3.9 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 ZXK.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>
/// 将数据存入prefs
/// </summary>
/// <param name="key"></param>
/// <param name="contents"></param>
public static void WriteAllTxtByPrefs(string key, string contents)
{
PlayerPrefs.SetString(key, 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)
{
Debug.LogError("WriteAllTxt Erro :" + ex.Message);
return;
}
}
/// <summary>
/// 读取txt文件中所有内容适宜小数据量
/// </summary>
/// <param name="txtFilePath">txt文件路径</param>
/// <returns>读取到的所有内容</returns>
public static string ReadAllTxt(string txtFilePath)
{
return File.ReadAllText(txtFilePath);
}
/// <summary>
/// 加载Resource路径下文本文档
/// </summary>
/// <param name="txtFilePath">相对于Resource文件夹路径</param>
/// <returns></returns>
public static string ReadAllTxtByResource(string txtFilePath)
{
return Resources.Load<TextAsset>(txtFilePath).text;
}
/// <summary>
/// 加载prefs下内容
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ReadAllTxtByPrefs(string key)
{
return PlayerPrefs.GetString(key,"");
}
/// <summary>
/// 读取txt文件中所有内容
/// </summary>
/// <param name="txtFilePath">txt文件路径</param>
/// <returns>读取到的所有行列内容</returns>
public static string[] ReadAllLines(string 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)
{
Debug.LogError("ReadBigTxt Erro :" + ex.Message);
return null;
}
return list.ToArray();
}
}
}