使用二进制实现本地数据保存和读取(C# , Unity)

举报
陈言必行 发表于 2021/08/13 22:26:17 2021/08/13
【摘要】 以二进制实现本地数据保存和读取,,, 项目中要存个本地文件,本来想使用Json 的,,可是怎么也没实现了,Json 好像只能存储string类型 -->(Dictionary<string, string> )1,,,没做出来,觉得XML读取费劲,最后决定使用二进制的形式存储为 .txt格式的,,,嗯,效果还可以,,,分享一下… 下面是存储和读...

以二进制实现本地数据保存和读取,,,

项目中要存个本地文件,本来想使用Json 的,,可是怎么也没实现了,Json 好像只能存储string类型 -->(Dictionary<string, string> )1,,,没做出来,觉得XML读取费劲,最后决定使用二进制的形式存储为 .txt格式的,,,嗯,效果还可以,,,分享一下…


下面是存储和读取的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;

public class LobbyMiddleRightView : MonoBehaviour { public GameObject DownLoadObj; private string filePath; private LocalDataObject localDataObject  = new LocalDataObject(); /// <summary> /// 初始化 /// </summary> public void Init() { filePath = Application.dataPath + "/StreamingFile" + "/LocalData.txt"; localDataObject.iconPos.Add(1, "AAA"); localDataObject.iconPos.Add(2, "BBB"); localDataObject.iconPos.Add(3, "CCC"); localDataObject.iconPos.Add(4, "DDD"); localDataObject.iconPos.Add(5, "EEE"); localDataObject.iconPos.Add(6, "FFF"); localDataObject.isDownLoad.Add(1,true); localDataObject.isDownLoad.Add(2,true); localDataObject.isDownLoad.Add(3,true); localDataObject.isDownLoad.Add(4,true); localDataObject.isDownLoad.Add(5,true); localDataObject.isDownLoad.Add(6,true); } /// <summary> /// 以二进制形式 保存到本地 /// </summary> private void Save() { //二进制格式化程序 BinaryFormatter bf = new BinaryFormatter(); //创建流文件 FileStream fileStream = File.Create(filePath); //序列化 bf.Serialize(fileStream, localDataObject); fileStream.Close(); if (File.Exists(filePath)) { Debug.Log("保存成功"); } } /// <summary> /// 读取本地文件 /// </summary> private void Read() { if (File.Exists(filePath)) { BinaryFormatter bf = new BinaryFormatter(); FileStream filestream = File.Open(filePath, FileMode.Open); LocalDataObject localDataObject = (LocalDataObject)bf.Deserialize(filestream); filestream.Close(); Debug.Log("本地文件读取成功:  "); foreach (var key in localDataObject.iconPos.Keys) { Debug.Log(key + "  "+ localDataObject.iconPos[key]); } foreach (var key in localDataObject.isDownLoad.Keys) { Debug.Log(key + "  " + localDataObject.isDownLoad[key]); } } else { Debug.Log("不存在存档"); } }
}

/// <summary>
/// 本地数据对象
/// </summary>
[Serializable]
public class LocalDataObject
{ //使用Json,只能存储string类型 -->(Dictionary<string, string> ) public Dictionary<int, string> iconPos = new Dictionary<int, string>(); public Dictionary<int, bool> isDownLoad = new Dictionary<int, bool>();
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
//写入到本地
	void Write(){ StreamWriter sw; FileInfo fi = new FileInfo(Application.persistentDataPath + filename+ ".txt"); sw = fi.CreateText(); sw.Write(getDefaultRuleString()); sw.Close(); sw.Dispose();
	}

	void Read(){ string readContext = "";//读取内容 StreamReader sr; FileInfo fi = new FileInfo(Application.persistentDataPath + filename+ ".txt"); if (fi.Exists) //若存在 { sr = fi.OpenText(); readContext = sr.ReadToEnd(); sr.Close(); sr.Dispose(); }
	}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

  1. 可能有别的方式实现我没找到吧 … ↩︎

文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。

原文链接:czhenya.blog.csdn.net/article/details/81531656

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。