Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在StreamingAssetPath上读写文件_C#_Android_Json_Unity3d_Web - Fatal编程技术网

C# 在StreamingAssetPath上读写文件

C# 在StreamingAssetPath上读写文件,c#,android,json,unity3d,web,C#,Android,Json,Unity3d,Web,这就是我在android中读取文本文件的方式 #if UNITY_ANDROID string full_path = string.Format("{0}/{1}",Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder); // Android only use WWW to read file WWW reader = new WWW(full_

这就是我在android中读取文本文件的方式

#if UNITY_ANDROID
string full_path = string.Format("{0}/{1}",Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
// Android only use WWW to read file
        WWW reader = new WWW(full_path);
        while (!reader.isDone){}

        json = reader.text;

        // PK Debug 2017.12.11
        Debug.Log(json);
 #endif
这就是我从电脑上读取文本文件的方式

#if UNITY_STANDALONE
        string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
        StreamReader reader = new StreamReader(full_path);
        json = reader.ReadToEnd().Trim();
        reader.Close();
#endif
现在我的问题是,我不知道如何在手机上写文件,因为我是在单机版上这样做的

#if UNITY_STANDALONE
        StreamWriter writer = new StreamWriter(path, false);
        writer.WriteLine(json);
        writer.Close();
 #endif
帮助任何人

更新问题 这是我需要获取的streamingasset文件夹中的json文件

现在我的问题是,我不知道如何在手机上写文件 因为我是在单机版上这样做的

#if UNITY_STANDALONE
        StreamWriter writer = new StreamWriter(path, false);
        writer.WriteLine(json);
        writer.Close();
 #endif
无法保存到此位置。是只读的。它是否在编辑器上工作并不重要。它是只读的,不能用于加载数据

从StreamingAssets读取数据

IEnumerator loadStreamingAsset(string fileName)
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);

    string result;

    if (filePath.Contains("://") || filePath.Contains(":///"))
    {
        WWW www = new WWW(filePath);
        yield return www;
        result = www.text;
    }
    else
    {
        result = System.IO.File.ReadAllText(filePath);
    }

    Debug.Log("Loaded file: " + result);
}
用法:

让我们从屏幕截图中加载“datacenter.json”文件:

void Start()
{
    StartCoroutine(loadStreamingAsset("datacenter.json"));
}


保存数据

IEnumerator loadStreamingAsset(string fileName)
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);

    string result;

    if (filePath.Contains("://") || filePath.Contains(":///"))
    {
        WWW www = new WWW(filePath);
        yield return www;
        result = www.text;
    }
    else
    {
        result = System.IO.File.ReadAllText(filePath);
    }

    Debug.Log("Loaded file: " + result);
}
保存在所有平台上工作的数据的路径是
Application.persistentDataPath
。在将数据保存到该路径之前,请确保在该路径内创建文件夹。您问题中的
StreamReader
可用于读取或写入此路径

保存到
应用程序。persistentDataPath
路径:

使用
File.writealBytes

应用程序.persistentDataPath
路径读取

使用
File.ReadAllBytes


有关如何在Unity中保存数据的完整示例,请参阅帖子。

这是我在没有
WWW
类(适用于Android和iOS)的情况下使用的方法,希望有用

public void WriteDataToFile(string jsonString)
{

    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }
    if (!File.Exists(filePath))
    {
        File.Create(filePath).Close();
        File.WriteAllText(filePath, jsonString);
    }
    else
    {
        File.WriteAllText(filePath, jsonString);
    }

}

无法进行实际验证,但您的独立代码应该可以在android上运行。你在手机上做这件事的方式相当于从网络上读取一个文件,而你不能在网络上写东西。所以在WWW课堂上没有实际的方法来做这件事@Draco18s
StreamWriter writer=新的StreamWriter(路径,false);WriteLine(json);writer.Close()
当我试图在手机上使用它时,我的catch(异常e)显示
System.IO.DirectoryNotFoundException:找不到路径的一部分“/jar:file:/data/app/com.steet383.rh.google-1/base.apk!/assets/notice.json”。
我的错误。我远离开发环境,无法检查任何东西。WWW不太可能允许你写作,但我不知道你的备选方案是什么。嗯,我什么都没有:(.我已经尝试了几个小时,我已经看到很多他们使用的是持久数据,但问题是我需要从aws服务器传递url并将其取回。这就是我的问题所在。它是否适用于WWW类,先生,因为我有一个json文件,其中包含我的数据中心
数据中心:{https://************.amazonaws.com}
我在我的独立平台上这样称呼它
serverListJsonURL=string.Format(“{0}/pc\u version/ph/check/serverList.json”,DataCenter\u BaseURL)
是,它使用
WWW
class@NoobProgrammer请记住,它将保存应用程序安装在设备上的文件(对于Android,它将保存在根数据文件夹中;对于Windows,它将保存在AppData文件夹中)您好,先生。我有点难以将其转换为以下内容:
StreamWriter writer=newstreamwriter(path,false);writer.WriteLine(json);writer.Close()
你能帮我解决问题的确切原因吗,我不明白这个问题吗?对不起,我的坏。我的眼睛现在变得模糊了,我很抱歉。因此,先生,我仍然可以通过这样做来呼叫我的s3服务器吗?因为在我的独立平台中,我像这样呼叫我的数据中心
https://********.amazonaws.com/pc\u version/ph/check/serverList.json
它将下载serverlist.json您知道,只要用
应用程序替换
完整路径值即可。persistentDataPath
,您的问题中使用
StreamWriter
的读写代码就可以了?这是程序员试图告诉您的。删除
UNITY\u ANDROID
,然后
UNITY\u STANDALONE
。您不需要这些。只需使用
StreamWriter
和此答案中提供的路径,您的问题就会解决。
StreamWriter
也可以在Android上工作。@PassetCronUs但问题是文件在我的streamingassetpath中?它会受到影响吗?类似的东西,先生。在我的StreamingAsset文件夹上呃,我有我的json文件顺便说一句,这个目录在Android上看起来是只读的,因为它被打包到一个jar中。但是,它在其他平台上是可写的。文档中的“只读”指的是变量,而不是目录(因为你不能更改路径)。