/******************************************************************** 文件: FileTools.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/03/29 17:28:19 最后修改: 梦语 最后修改时间: 2024/04/03 15:59:21 功能: 文件读写工具 *********************************************************************/ using System; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; public static class FileTools { /// /// 从文件中读取Json /// public static bool ReadFileForJson(string filePath, out T data) { try { data = JsonTools.FromJson(ReadFile(filePath)); return data != null; } catch (Exception) { //Debug.LogWarningFormat("文件读取错误,路径:{0}", filePath); data = default; return false; } } ///// ///// 从文件中读取Json ///// //public static bool ReadFileForJson(Type type, string filePath, out object data) //{ // try // { // //data = JsonConvert.DeserializeObject(ReadFile(filePath)); // data = JsonMapper.ToObject(ReadFile(filePath), type); // return data != null; // } // catch (Exception) // { // Debug.LogWarningFormat("文件读取错误,路径:{0}", filePath); // data = default; // return false; // } //} /// /// 从文件中读取字符串 /// public static string ReadFile(string filePath) { try { Debug.Log("当前读取路径:" + filePath); if (!File.Exists(filePath)) { //Debug.LogWarningFormat("该文件不存在,路径:{0}", filePath); return null; } using (FileStream fs = new FileStream(filePath, FileMode.Open)) { StreamReader reader = new StreamReader(fs); string data = reader.ReadToEnd(); reader.Close(); fs.Close(); return data; } } catch (Exception) { //Debug.LogWarningFormat("文件读取错误,路径:{0}", filePath); return null; } } /// /// 向文件中写入Json /// public static bool WriteFileForJson(string filePath, T classData, bool isAppend = false, bool isFormat = false) where T : new() { string data = JsonTools.ToJson(classData, isFormat); //string data = JsonMapper.ToJson(classData); return WriteFile(filePath, data, isAppend); } /// /// 向文件中写入字符串 /// public static bool WriteFile(string filePath, string data, bool isAppend = false) { try { //Debug.Log("当前写入路径:" + filePath); FileStream fs; if (!File.Exists(filePath)) { string dirPath = Path.GetDirectoryName(filePath); Directory.CreateDirectory(dirPath); fs = new FileStream(filePath, FileMode.Create); } else { if (!isAppend) fs = new FileStream(filePath, FileMode.Truncate); else fs = new FileStream(filePath, FileMode.Append); } StreamWriter writer = new StreamWriter(fs); writer.Write(data); writer.Close(); fs.Close(); return true; } catch (Exception) { //Debug.LogErrorFormat("文件写入错误,路径:{0}", filePath); return false; } } public static bool FileExists(string filePath) { return File.Exists(filePath); } public static void CopyFile(string filePath, string targetPath) { if (File.Exists(filePath)) { string dirPath = Path.GetDirectoryName(targetPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } File.Copy(filePath, targetPath, true); } } public static void FindAllFileInDirectory(string dirPath, List saveList, params string[] types) { // 获取文件夹内所有文件和文件夹 string[] files = Directory.GetFiles(dirPath); string[] dirs = Directory.GetDirectories(dirPath); // 遍历文件 foreach (string file in files) { bool isNeed = false; // 检查文件是否为需要的类型 foreach (string type in types) { if (file.EndsWith(type)) { isNeed = true; break; } } if (isNeed) { saveList.Add(file.Replace("\\", "/")); } } // 递归遍历子文件夹 foreach (string dir in dirs) { FindAllFileInDirectory(dir, saveList, types); } } /// /// 判断文件夹是否为空 /// /// /// public static bool CheckIfDirectoryIsEmpty(string dirPath) { // 检查文件夹是否存在 if (Directory.Exists(dirPath)) { // 获取文件夹中的所有文件和子文件夹 string[] files = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories); DirectoryInfo dirInfo = new DirectoryInfo(dirPath); DirectoryInfo[] subDirs = dirInfo.GetDirectories(); // 判断文件和子文件夹的数量 if (files.Length == 0 && subDirs.Length == 0) { return true; } else { return false; } } else { return false; } } }