本地存储

public FileStream(string path, FileMode mode)

FileMode成员:

(1)Append 打开现有文件并查找到文件尾,或创建新文件。FileMode.Append 只能同 FileAccess.Write 一起使用。  

(2)Create 创建新文件(如果文件不存在)。如果文件已存在,它将被改写。这要求 FileIOPermissionAccess.Write。FileMode.Create 等效于这样的 请求:如果文件不存在,则使用 CreateNew;否则使用 Truncate。  

(3)CreateNew 创建新文件。此操作需要 FileIOPermissionAccess.Write 。如果文件已存在,则将引发 IOException。     

(4)Open 打开现有文件。打开文件的能力取决于 FileAccess 所指定的值。如果该文件不存在,则引发 System.IO.FileNotFoundException。  
(5)OpenOrCreate 打开文件(如果文件存在);否则,创建新文件。如果用 FileAccess.Read 打开文件,则需FileIOPermissionAccess.Read。  

(6)Truncate 打开现有文件。文件一旦打开就将被截断为零字节大小。此操作需要FileIOPermissionAccess.Write。试图从使用Truncate 打开的文件中进行读取将导致异常。 

FileStream fs = new FileStream(@"D:\a.txt", FileMode.Create);  

public FileStream(string path, FileMode mode, FileAccess access)

使用指定的路径、创建模式和读/写权限初始化 FileStream 类的新实例。

 FileAccess成员:

(1)Read 对文件的读访问。可从文件中读取数据。同 Write 组合即构成读写访问权。      

(2)ReadWrite 对文件的读访问和写访问。可从文件读取数据和将数据写入文件。   

(3)Write 文件的写访问。可将数据写入文件。同 Read 组合即构成读/写访问权。   

FileStream fs = 

new FileStream(@"D:<span style="font-size:12px;">\a.txt", FileMode.Create, FileAccess.ReadWrite);



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
using LitJson;
using UnityEngine.UI;
using System.Text;

public class Manager : MonoBehaviour
{
    string path;
    AssetBundle a;
    Texture2D t;
    string str;
    Sprite s;
    public GameObject cube;
    IEnumerator Start()
    {
        //WWW www = new WWW(path);
        //yield return www;
        //a = www.assetBundle;
        //t = www.texture;
        //if (www != null && string.IsNullOrEmpty(www.error))
        //{
        //    s = Sprite.Create(t, new Rect(0, 0, t.width, t.height), Vector2.zero);
        //}
        #region www
        WWW www1 = new WWW("file://" + Application.dataPath + "/Pic/airplane.png");
        yield return www1;
        Texture2D texture2D1 = www1.texture;
        cube.GetComponent<Renderer>().material.mainTexture = texture2D1;
        #endregion
        #region Xml Create
        //XmlDocument xml = new XmlDocument();

        //XmlReaderSettings set = new XmlReaderSettings();
        //set.IgnoreComments = true;//忽视xml的注释文档
        //xml.Load(XmlReader.Create(path, set));


        //XmlDocument xml1 = new XmlDocument();

        //WWW www1 = new WWW(path);
        //yield return www1;
        //xml1.LoadXml(www1.text); 
        #endregion

        byte[] b = Encoding.UTF8.GetBytes("");
        string s = Encoding.UTF8.GetString(b);



        string json = @"
            {
                ""Name""     : ""Thomas More"",
                ""Age""      : 57,
                ""Birthday"" : ""02/07/1478 00:00:00""
            }";

        JsonData jd = JsonMapper.ToObject(json);
        //print("Name=" + (string)jd["Name"]);
        //print("Age=" + (int)jd["Age"]);
        //print("Birthday=" + (string)jd["Birthday"]);




        Object prefab = Resources.Load("MyPrefab/Prefab01");
        TextAsset textAsset = (TextAsset)Resources.Load("Json01");
        jd = JsonMapper.ToObject(textAsset.text);
        print(textAsset.text);
        print("Name=" + (string)jd["Name"]);
        print("Age=" + (int)jd["Age"]);
        print("Birthday=" + (string)jd["Birthday"]);
        print("Thumbnail[0]=" + (string)jd["Thumbnail"][0]["Url"]);
        print("Thumbnail[1][Height][Width]=" + (string)jd["Thumbnail"][1]["Height"] + " " + (string)jd["Thumbnail"][1]["Width"]);
        print("SelfSpeak=" + (string)jd["SelfSpeak"]);
        print(jd["Thumbnail"].Count);

        //存数据
        JsonWriter writer = new JsonWriter();
        writer.WriteArrayStart();
        writer.Write("one");
        writer.Write("one1");
        writer.Write("one2");
        writer.Write("one3");
        writer.WriteArrayEnd();
        #region JsonWriter
        //存数据1
        StringBuilder sb = new StringBuilder();
        writer = new JsonWriter(sb);

        writer.WriteObjectStart();

        writer.WritePropertyName("Name");
        writer.Write("yusong");

        writer.WritePropertyName("Age");
        writer.Write(26);

        writer.WritePropertyName("Girl");

        writer.WriteArrayStart();

        writer.WriteObjectStart();
        writer.WritePropertyName("name");
        writer.Write("ruoruo");
        writer.WritePropertyName("age");
        writer.Write(24);
        writer.WriteObjectEnd();

        writer.WriteObjectStart();
        writer.WritePropertyName("name");
        writer.Write("momo");
        writer.WritePropertyName("age");
        writer.Write(26);
        writer.WriteObjectEnd();

        writer.WriteArrayEnd();

        writer.WriteObjectEnd();
        Debug.Log(sb.ToString());

        #endregion
        jd = JsonMapper.ToObject(sb.ToString());
        Debug.Log("name = " + (string)jd["Name"]);
        Debug.Log("Age = " + (int)jd["Age"]);
        JsonData jdItems = jd["Girl"];
        for (int i = 0; i < jdItems.Count; i++)
        {
            Debug.Log("Girl name = " + jdItems[i]["name"]);
            Debug.Log("Girl age = " + (int)jdItems[i]["age"]);
        }



        print("................................................................");
        print(sb.ToString());
        string JsonStringData = sb.ToString();

        string str = JsonMapper.ToJson(jd);
        print(str);
        byte[] bts = Encoding.UTF8.GetBytes(str);
        print(sb.GetType());
        print(Application.dataPath);
        string FolderName = Application.dataPath + "/MyFolderExp";
        //创建文件夹
        if (!Directory.Exists(FolderName))
        {
            print(111111);
            Directory.CreateDirectory(FolderName);
            print(Directory.Exists(FolderName));
            print(456);
        }
        //创建文件
        if (!File.Exists(FolderName + "/myJson.json"))
        {
            File.Create(FolderName + "/myJson.json");
            print(File.Exists(FolderName + "/myJson.json"));
        }
        FileInfo fileInfo = new FileInfo(FolderName + "/myJson.json");
        //print(fileInfo.ToString());
        //FileStream fs = fileInfo.OpenRead();print(fs);
        FileStream fileInfo1 = new FileStream(FolderName + "/myJson.json", FileMode.Open);
        fileInfo1.Write(bts, 0, bts.Length);
        if (fileInfo1 != null)
        {
            fileInfo1.Close();
        }
        FileStream file = new FileStream(FolderName + "/myJson1.json", FileMode.Create);
        //FileStream file1 = new FileStream(FolderName + "/myJson1.json", FileMode.OpenOrCreate);
        file.Write(bts, 0, bts.Length);
        if (file != null)
        {
            file.Close();
        }

        //path = Application.dataPath + "/MyFolderExp/myJson1.json";
        //FileStream myfs = new FileStream(path,FileMode.Append);//写入 增加
        //byte[] myByte = System.Text.Encoding.UTF8.GetBytes("123456");
        //myfs.Write(myByte, 0, myByte.Length);

        //FileStream myfs1 = new FileStream(path, FileMode.Open);//读取
        //int myfsLen = (int)myfs1.Length;
        //myByte = new byte[myfsLen];
        //int r = myfs1.Read(myByte, 0, myByte.Length);
        //string str3 = System.Text.Encoding.UTF8.GetString(myByte);
        //print(str3);

        if (File.Exists(path))
        {
            StreamWriter streamwrite = new StreamWriter(path);
            streamwrite.WriteLine("木子屋");
            streamwrite.WriteLine("http://www.mzwu.com/");
            streamwrite.Write("2008-04-13");
            streamwrite.Close();

            StreamReader streamreader = new StreamReader(path);
            print(streamreader.ReadLine());
            print(streamreader.ReadToEnd());
            streamreader.Close();
        }
        //存数据2
        //JSONObject jsonobject = new JSONObject();
        //Unity 保存Json数据到本地文件http://blog.csdn.net/u014076894/article/details/52238348



        string jsonData = System.Text.Encoding.UTF8.GetString(textAsset.bytes, 3, textAsset.bytes.Length - 3);//这句话的效果其实就是在Notepad++中以UTF-8无BOM格式编码相同的效果
        //print(textAsset.text);
        jd = JsonMapper.ToObject(textAsset.text);
        JsonReader Reader = new JsonReader(textAsset.text);
        //while(Reader.Read())
        //{
        //    print(Reader.Token )
        //}
        //JsonReaderWriterFactory


        string str1 = textAsset.text;
        //print(jd);
        //print(str);
        Texture2D texture2D = Resources.Load("Test1") as Texture2D;
        Sprite sprite = Resources.Load("Test1") as Sprite;

        //FileInfo file = new FileInfo(path);
        //if (file.Exists) { }
        //if (file != null) { }
        #region StreamReader读取文件(Json)  太繁琐,老旧,弃用
        //StreamReader sr = File.OpenText(Application.dataPath + "/Resources/Json1.json");
        //StringBuilder jsonArrayText_tmp = new StringBuilder();
        //string input = null;
        //while ((input = sr.ReadLine()) != null)
        //{
        //    jsonArrayText_tmp.Append(input);
        //}
        //sr.Close();
        //string jsonArrayText = jsonArrayText_tmp.Replace(" ", "").ToString();
        #endregion
        #region Resources.Load读取文件
        //TextAsset textasset = Resources.Load<TextAsset>("Json1");
        //JsonData jsondata = JsonMapper.ToObject(textasset.text);
        #endregion





    }
    public void odd()
    {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值