using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using UnityEngine; using UnityEngine.Networking; namespace MyFrameworkPure { /// /// 文件处理工具 /// public static class FileTool { /// /// 以UTF8编码读取文件内容 /// /// /// public static string ReadAllText(string path) { if (path.StartsWith(Application.streamingAssetsPath) && Application.platform == RuntimePlatform.Android) { UnityWebRequest www = UnityWebRequest.Get(path); www.SendWebRequest(); while (!www.isDone) { } return www.downloadHandler.text; } return ReadAllText(path, Encoding.UTF8); } /// /// 读取文件内容 /// public static string ReadAllText(string path, Encoding encoding) { string text = ""; try { using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs, encoding)) { text = sr.ReadToEnd(); } //text = File.ReadAllText(path, encoding); } catch (Exception e) { Debug.Log(e); } return text; } public static string[] ReadAllLines(string path) { string[] lines = new string[] { }; try { lines = File.ReadAllLines(path); } catch (Exception e) { Debug.LogException(e); } return lines; } public static string[] SafeReadAllLines(String path) { using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs,Encoding.UTF8)) { List file = new List(); while (!sr.EndOfStream) { file.Add(sr.ReadLine()); } return file.ToArray(); } } public static string[,] ReadCsvData(string path) { string str = ReadAllText(path); string[] lines = str.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); string[,] data = ReadCsvFromLines(lines); return data; } public static string[,] ReadCsvData(byte[] bytes) { string[,] data = new string[,] { }; try { string str = Encoding.UTF8.GetString(bytes); string[] lines = str.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); ; data = ReadCsvFromLines(lines); } catch (Exception e) { Debug.Log(e); } return data; } static string[,] ReadCsvFromLines(string[] lines) { int row = lines.Length; int col = lines.Max(x => x.Split(',').Length); string[,] data = new string[row, col]; for (int i = 0; i < lines.Length; i++) { string[] splits = lines[i].Split(','); for (int j = 0; j < splits.Length; j++) { data[i, j] = splits[j]; } } return data; } /// /// 从远程地址读取文本信息 /// /// /// /// public static void ReadTextFromUrl(string url, Encoding encoding,Action action) { CoroutineTool.DoCoroutine(AsyncReadAllText(url, encoding, action)); } /// /// 异步读取文本 /// /// /// /// /// public static IEnumerator AsyncReadAllText(string path, Encoding encoding, Action action) { using (WWW www = new WWW(path)) { yield return www; if (string.IsNullOrEmpty(www.error)) { string text = encoding.GetString(www.bytes); if (action != null) action(text); } else { Debug.LogError(www.error); } } } /// /// 写入文件,如果文件存在则覆盖,不存在则创建 /// /// /// public static void WriteAllText(string path, string contents) { try { File.WriteAllText(path, contents); } catch (Exception e) { Debug.LogException(e); } } /// /// 远程读取图片信息 /// /// /// /// public static void ReadTextureFromUrl(string url, Action action,Action onError = null) { CoroutineTool.DoCoroutine(AsyncReadTextureFromUrl(url, action,onError)); } /// /// 异步读取图片 /// /// /// /// /// public static IEnumerator AsyncReadTextureFromUrl(string url, Action action,Action onError = null) { Debug.Log(url); using (WWW www = new WWW(url)) { yield return www; if (string.IsNullOrEmpty(www.error)) { Texture2D texture = new Texture2D(0, 0); texture.LoadImage(www.bytes); if (action != null) action(texture); } else { onError?.Invoke(www.error); Debug.LogError(www.error); } } } /// /// 读取贴图文件 /// /// /// public static Texture2D ReadTextureFromFile(string path) { if (!System.IO.File.Exists(path)) { Debug.LogWarning(path + "不存在!"); return null; } byte[] bytes = System.IO.File.ReadAllBytes(path); Texture2D texture = new Texture2D(0, 0); texture.LoadImage(bytes); return texture; } /// /// 读取远程音频信息 /// /// /// public static void ReadAudioFromUrl(string url, Action action) { CoroutineTool.DoCoroutine(AsyncReadAudioFromUrl(url, action)); } public static IEnumerator AsyncReadAudioFromUrl(string url,Action action) { Debug.Log(url); using (WWW www = new WWW(url)) { yield return www; if (string.IsNullOrEmpty(www.error)) { if (action != null) action(www.GetAudioClip(false)); } else { Debug.LogError(www.error); } } } /// /// 获取文件编码 /// /// /// public static Encoding GetEncoding(string filename) { var bom = new byte[4]; using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read)) { file.Read(bom, 0, 4); } if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7; if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) return Encoding.UTF8; if (bom[0] == 0xff && bom[1] == 0xfe) return Encoding.Unicode; //UTF-16LE if (bom[0] == 0xfe && bom[1] == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32; return Encoding.ASCII; } /// /// 重命名文件 /// /// /// public static void Rename(string fileName, string newFileName) { FileInfo fi = new FileInfo(fileName); fi.MoveTo(newFileName); } } }