using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO; using System.Text; namespace YKMoon.IO { public partial class IOTools { #region Get public static byte[] GetFileBytes(string fullPath) { try { if(!File.Exists(fullPath)) return null; FileStream fs = File.OpenRead(fullPath); if(fs == null) throw new Exception(string.Format("FileStream is null file : {0}", fullPath)); long length = fs.Length; byte[] buffer = new byte[length]; fs.Read(buffer, 0, (int)length); fs.Close(); return buffer; } catch(Exception e) { Debug.LogErrorFormat("GetFileBytes is error : {0}", e.ToString()); } return null; } public static string GetFileStringByPath(string fullPath) { try { if(!File.Exists(fullPath)) return null; return Encoding.UTF8.GetString(File.ReadAllBytes(fullPath)); } catch(Exception e) { Debug.LogErrorFormat("GetFileStringByPath is error : {0}", e.ToString()); } return ""; } public static FileStream GetFileStream(string fullPath) { try { if(!File.Exists(fullPath)) return null; FileStream fs = File.OpenRead(fullPath); if(fs == null) throw new Exception(string.Format("FileStream is null file : {0}", fullPath)); return fs; } catch(Exception e) { Debug.LogErrorFormat("GetFileStream is error : {0}", e.ToString()); } return null; } #endregion #region Save public static void CreateFile(string fileFullPath, byte[] buffer) { if(string.IsNullOrEmpty(fileFullPath)) return; string path = Path.GetDirectoryName(fileFullPath).Replace("\\", "/"); if(!string.IsNullOrEmpty(path) && !Directory.Exists(path)) Directory.CreateDirectory(path); if(File.Exists(fileFullPath)) File.Delete(fileFullPath); FileStream fs = new FileStream(fileFullPath, FileMode.Create); fs.Write(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); } #endregion /// 删除多个文件// public static void DelFiles(string sourceFolder, string type, List detach) { if(!System.IO.Directory.Exists(sourceFolder)) return; string[] files = System.IO.Directory.GetFiles(sourceFolder, type); for(int i = 0; i < files.Length; i++) { string file = files[i]; string name = System.IO.Path.GetFileName(file); if(detach == null || !detach.Contains(name)) System.IO.File.Delete(file); } } /// 删除多个文件// public static void DelFiles(string sourceFolder, string type) { if(!System.IO.Directory.Exists(sourceFolder)) return; string[] files = System.IO.Directory.GetFiles(sourceFolder, type); for(int i = 0; i < files.Length; i++) { string file = files[i]; System.IO.File.Delete(file); } } /// 删除单个文件// public static void DelFile(string sourceFile) { if(File.Exists(sourceFile)) File.Delete(sourceFile); } } }