using UnityEngine; using System; using System.Text; using System.IO; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using GameCore; namespace ResUpdate { public class FileUtil { /// /// 打开文件流,如果不存在,就创建一个文件 /// /// /// public static Stream ForceOpenFileStream(string filePath) { var directory = Path.GetDirectoryName(filePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } Stream fileStream = null; try { fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); } catch (Exception e) { if (fileStream != null) { fileStream.Close(); fileStream = null; } if (BaseLogger.isDebug) Debug.LogWarning(string.Format("Failed to force open file stream: {0}, error {1}!", filePath, e.Message)); } return fileStream; } /// /// 加载文件 /// /// /// public static string LoadTextFile(string filePath) { string text = string.Empty; if (File.Exists(filePath)) { text = File.ReadAllText(filePath); } else { if (BaseLogger.isDebug) Debug.LogWarning("Load text file is not exist: " + filePath); } return text; } /// /// 保存文件 /// /// /// public static void SaveTextFile(string filePath, string content) { var stream = ForceOpenFileStream(filePath); if (stream == null) return; var writer = new StreamWriter(stream); writer.Write(content); writer.Close(); stream.Close(); } /// /// 打开文件流 /// /// /// public static Stream OpenFileStream(string filePath) { Stream fileStream = null; try { fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); } catch (Exception e) { if (fileStream != null) { fileStream.Close(); fileStream = null; } if (BaseLogger.isDebug) Debug.LogWarning(string.Format("Failed to open file stream: {0}, error {1}", filePath, e.Message)); } return fileStream; } /// /// 创建一个目录 /// /// public static void CreateDirectory(string dirPath) { if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath); } /// /// 获取文件扩展名 /// /// /// public static string GetFileSuffix(string fileName) { var index = fileName.LastIndexOf('.'); if (index == -1) return string.Empty; return fileName.Substring(index); } public static string GetFilePathWithoutSuffix(string fileName) { var index = fileName.LastIndexOf('.'); if (index == -1) return fileName; else return fileName.Substring(0, index); } /// /// 移动文件 /// /// /// public static void MoveFile(string srcFilePath, string destFilePath) { if (File.Exists(srcFilePath)) { if (File.Exists(destFilePath)) File.Delete(destFilePath); File.Move(srcFilePath, destFilePath); } } /// /// 删除文件 /// /// /// public static bool DeleteFile(string path) { if (!File.Exists(path)) { return false; } try { File.Delete(path); return true; } catch (System.Exception ex) { if (BaseLogger.isDebug) Debug.LogWarning(string.Format("Failed to deleteFile file {0}: {1}", path, ex.Message)); return false; } } /// /// 拷贝文件 /// /// /// /// public static IEnumerator CopyFile(string srcfile, string destfile) { if (srcfile.Contains("jar:")) { WWW www = new WWW(srcfile); yield return www; File.WriteAllBytes(destfile, www.bytes); www.Dispose(); yield break; } else { File.Copy(srcfile, destfile); } yield break; } public static long GetFileBytesSize(string filePath) { if (!File.Exists(filePath)) return 0; long byteSize = 0; var stream = OpenFileStream(filePath); if (stream != null) { byteSize = stream.Length; stream.Close(); } return byteSize; } public static string[] GetAllFiles(string rootPath) { if (!Directory.Exists(rootPath)) return new string[] { }; return Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories); } } }