C#实现文件的读写的两个方法

csharp

浏览数:254

2019-1-7

AD:资源代下载服务
    private void WriteTestFile(string filename, string data)
    {
        FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);

        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
        fs.Write(buffer, 0, buffer.Length);

        fs.Close();
    }

    private string ReadTestFile(string filename)
    {
        FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, (int)fs.Length);
        fs.Close();

        string str = System.Text.Encoding.UTF8.GetString(buffer);
        return str;
    }

    public static string LoadFile(string filePath){
      string url = Application.streamingAssetsPath + "/" + filePath;
      #if UNITY_EDITOR
      return File.ReadAllText(url);
      #elif UNITY_ANDROID
      WWW www = new WWW(url);
      while (!www.isDone) { }
       return www.text;
      #endif
      }