89 lines
2.4 KiB
C#
Raw Normal View History

2025-03-11 16:24:25 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
namespace ZXKFramework
{
public class TextTools
{
//<2F><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
public static void Create(string path, string info)
{
StreamWriter sw;
FileInfo fi = new FileInfo(path);
sw = fi.CreateText();
sw.WriteLine(info);
sw.Close();
sw.Dispose();
}
//<2F><>ȡ
public static void Read(string sPath, Action<bool, string> callBack)
{
StreamReader sr = null;
sr = File.OpenText(sPath);
string t_Line;
if ((t_Line = sr.ReadLine()) != null)
{
callBack?.Invoke(true, t_Line);
}
else
{
callBack?.Invoke(false, "<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʧ<EFBFBD>ܻ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>:" + sPath);
}
sr.Close();
sr.Dispose();
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public static string Read(string sPath)
{
if (File.Exists(sPath))
{
return File.ReadAllText(sPath, Encoding.UTF8);
}
return "";
}
//<2F>õ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
public static string[] ReadAllLines(string path)
{
return File.ReadAllLines(path);
}
//<2F>õ<EFBFBD>Text<78><74>ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>û<EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD>
public static List<string> ReadAllLinesNoNull(string path)
{
List<string> result = new List<string>();
string[] res = File.ReadAllLines(path);
for (int i = 0; i < res.Length; i++)
{
if (!String.IsNullOrEmpty(res[i]))
{
result.Add(res[i]);
}
}
return result;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ɳ<EFBFBD><C9B3>·<EFBFBD><C2B7>
public static bool CopyTxt(string destFileName)
{
TextAsset text = Resources.Load<TextAsset>(destFileName);
if (text == null)
{
WDebug.Log(false, "Resources <20>²<EFBFBD><C2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD> " + destFileName);
return false;
}
string path = Application.persistentDataPath + "/" + destFileName + ".txt";
if (!File.Exists(path))
{
Create(path, text.text);
}
return true;
}
}
}