using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; public class Utils_File { Utils_File() { } private static Utils_File _instance; public static Utils_File instance { get { if (_instance == null) { _instance = new Utils_File(); } return _instance; } } #region File public void File_Create(string path) { if (!File.Exists(path)) { FileStream fs = new FileStream(path, FileMode.OpenOrCreate); fs.Dispose(); } } public string File_Read(string path) { string content = string.Empty; if (File.Exists(path)) { FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs); content = sr.ReadToEnd(); sr.Dispose(); fs.Dispose(); } return content; } public List<string> File_Read_Line(string path) { List<string> contents = new List<string>(); if (File.Exists(path)) { FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs); string content = string.Empty; while (sr.Peek() != -1) { content = sr.ReadLine(); contents.Add(content); } sr.Dispose(); fs.Dispose(); } return contents; } public void File_Write(string path, string content, bool append = false, bool newLine = false) { FileMode mode = FileMode.OpenOrCreate; if (File.Exists(path) && append) { mode = FileMode.Append; } FileStream fs = new FileStream(path, mode); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.Write(content); if (newLine) { sw.WriteLine(""); } sw.Dispose(); fs.Dispose(); } public void File_Write_Line(string path, List<string> contents, bool append = false) { FileMode mode = FileMode.OpenOrCreate; if (File.Exists(path) && append) { mode = FileMode.Append; } FileStream fs = new FileStream(path, mode); StreamWriter sw = new StreamWriter(fs, Encoding.Default); for (int i = 0; i < contents.Count; i++) { sw.WriteLine(contents[i]); } sw.Dispose(); fs.Dispose(); } public void File_Delete(string path) { if (File.Exists(path)) { File.Delete(path); } } public void File_Copy(string path_o, string path_n) { if (File.Exists(path_o)) { File_Delete(path_n); File.Copy(path_o, path_n, true); } } public void File_Move(string path_o, string path_n) { if (File.Exists(path_o)) { File.Move(path_o, path_n); } } public bool File_Contain(string path) { return File.Exists(path); } public void File_Find(string path) { DirectoryInfo dicInfo = new DirectoryInfo(path); FileInfo[] _files = dicInfo.GetFiles(); List<FileInfo> files = _files.ToList<FileInfo>(); foreach (FileInfo file in files) { UnityEngine.Debug.LogError(file.Name); } } #endregion #region Directory public void Dic_Create(string path) { if (!Directory.Exists(path)) { DirectoryInfo dic = Directory.CreateDirectory(path); } } public void Dic_Delete(string path) { if (Directory.Exists(path)) { Directory.Delete(path); } } public void Dic_Move(string path_o,string path_n) { if (Directory.Exists(path_o)) { Directory.Move(path_o, path_n); } } public bool Dic_Contain(string path) { return Directory.Exists(path); } public void Dic_Find(string path) { string[] dirs = Directory.GetDirectories(path); foreach (string dir in dirs) { UnityEngine.Debug.LogError(dir); } } #endregion }
文件:建立,讀取,寫入,複製,移動,刪除,目錄下全部文件app
文件夾:建立,移動,刪除,目錄下全部文件夾ide
功能註釋:this
1.peek 是用來肯定你read的文件是否結束了,若是結束了會返回int型 -1 spa
2.flush 使用此方法將全部信息從基礎緩衝區移動到其目標或清除緩衝區,或者同時執行這兩種操做(沒使用)code
3.FileStream類的Close()方法是繼承於Stream類的,源代碼是這樣的對象
public virtual void Close()
{
Dispose(true);
GC.SuppressFinalize(this);
}
FileStream類的Dispose()方法是繼承於Stream類的,源代碼是這樣的:
public void Dispose()
{
Close();
} blog
是一個標準的Dispose模式的實現,Close()方法調用的是帶參數的Dispose方法,而後調用GC.SuppressFinalize (this);請求系統不要調用指定對象的終結器。而Dispose()方法直接調用Close()方法!繼承
對於FileStream類來講,Close()方法和Dispose()方法是沒有區別!接口
4.using語句提供了一個脈絡清晰的機制來控制資源的生存期,建立的對象會在using語句結束時被摧毀,使用前提該對象必須繼承了IDisposable接口。資源