1. 程式人生 > >Unity中刪除文件目錄下的所有文件和查看文件裏面的內容

Unity中刪除文件目錄下的所有文件和查看文件裏面的內容

nds get -s streaming filename exceptio director pat sts

這個腳本中存儲了:

刪除文件夾中所有的文件的方法,

刪除單個文件的方法

獲取文本內容的方法

獲取其他類型文本內容的方法

寫入文本文件的方法

void Start () {
    string filePath = Application.streamingAssetsPath + "/" + "abb.txt";
 
    #region 進行文件的刪除
    //string fullPath = Application.streamingAssetsPath;
    //bool isTrue = DeleteAllFile(fullPath);
    //if (isTrue)
    //{
    
// Debug.Log("刪除成功!!!!!!"); //} //else //{ // Debug.Log("刪除失敗!!!!!!"); //} #endregion #region 進行文件內容的獲取的第一種方式 //string m_Str = GainFileContent_1(filePath); //Debug.Log(m_Str); #endregion #region 進行文件內容的獲取的第二種方式 //string m_Str = GainFileContent_2(filePath);
//Debug.Log(m_Str); #endregion #region 進行文件內容的獲取的第三種方式 string m_Str = GainFileContent_3(filePath); Debug.Log(m_Str); #endregion #region 進行文件內容的寫入 bool isTrue = WriteFileContent_3(filePath); if (isTrue) { Debug.Log("寫入成功!!!!!!"); } else { Debug.Log(
"寫入失敗!!!!!!"); } #endregion } /// <summary> /// 刪除指定文件目錄下的所有文件 /// </summary> /// <param name="fullPath">文件路徑</param> public bool DeleteAllFile(string fullPath) { //獲取指定路徑下面的所有資源文件 然後進行刪除 if (Directory.Exists(fullPath)) { DirectoryInfo direction = new DirectoryInfo(fullPath); FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories); Debug.Log(files.Length); for (int i = 0; i < files.Length; i++) { if (files[i].Name.EndsWith(".meta")) { continue; } string FilePath = fullPath + "/" + files[i].Name; print(FilePath); File.Delete(FilePath); } return true; } return false; } /// <summary> /// 第一種:獲取指定文件中的內容 /// </summary> public string GainFileContent_1(string filePath) { string m_Str = null; try { //讀取文件的所有行,並將數據讀取到定義好的字符數組strs中,一行存一個單元 string[] strs = File.ReadAllLines(filePath); for (int i = 0; i < strs.Length; i++) { m_Str += strs[i];//讀取每一行,並連起來 m_Str += "\n";//每一行末尾換行 } return m_Str; } catch (System.Exception e) { Debug.Log(e.Message); return m_Str; } } /// <summary> /// 從一個文本文檔或非txt文本文檔中獲取內容 /// </summary> /// <param name="m_FileName">文件的路徑和名字</param> public string GainFileContent_2(string m_FileName) { try { string pathSource = m_FileName; using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read)) { byte[] bytes = new byte[fsSource.Length]; int numBytesToRead = (int)fsSource.Length; int numBytesRead = 0; while (numBytesToRead > 0) { int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); if (n == 0) break; numBytesRead += n; numBytesToRead -= n; } numBytesToRead = bytes.Length; string m_Str = UTF8Encoding.UTF8.GetString(bytes); return m_Str; } } catch(System.Exception e) { Debug.Log(e.Message); return null; } } /// <summary> /// 進行一個文本的讀取內容 /// </summary> /// <param name="FilePath"></param> /// <returns></returns> public string GainFileContent_3(string FilePath) { // ReadAllText方法第一個參數是要讀取txt文件的路徑,第二個參數是編碼方式,這裏采用默認 string m_Str = File.ReadAllText(FilePath, Encoding.Default); return m_Str; } /// <summary> /// 將一個文件寫入到文本當中 /// </summary> /// <param name="FilePath"></param> /// <returns></returns> public bool WriteFileContent_3(string FilePath) { try { File.AppendAllText(FilePath, "我被寫進來了", Encoding.Default); return true; } catch (System.Exception e) { Debug.Log(e.Message); return false; } }

以上實現了對於文本文件的增,刪,查,希望能幫助到大家!!!!!!

Unity中刪除文件目錄下的所有文件和查看文件裏面的內容