二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
来源:互联网 发布:python splinter 安装 编辑:程序博客网 时间:2024/06/08 19:24
网上找到一些方法,二进制序列化现对于json不可视,稍加改进封装了下,代码具体如下
using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;using System;using System.Runtime.Serialization.Formatters.Binary;public class SerializeTHFile{ public static void SerializeMethod<T>(T tempSerializeList) // 二进制序列化 { //路径 string path = Application.isEditor ? Application.persistentDataPath.Replace("/" + Application.productName, "") : Application.persistentDataPath; path += "/保存文件名称" ; FileStream fs = new FileStream(path, FileMode.Create); try { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, tempSerializeList); fs.Close(); } catch (Exception ex) { fs.Close(); Debug.Log(ex); } } public static T DeserializeMethod<T>() // 二进制反序列化 { T tempDeserializeList ; string path = Application.isEditor ? Application.persistentDataPath.Replace("/" + Application.productName, "") : Application.persistentDataPath; path += "/读取文件名称" ; FileInfo binaryFile = new FileInfo(path); if (!binaryFile.Exists) { Debug.Log("反序列化文件不存在"); return default(T); } FileStream fs = new FileStream(path, FileMode.Open); try { BinaryFormatter bf = new BinaryFormatter(); tempDeserializeList = (T)bf.Deserialize(fs); fs.Close(); return tempDeserializeList; } catch (Exception ex) { Debug.LogWarning(ex); fs.Close(); File.Delete(path); return default(T); } }}
阅读全文
0 0
- 二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
- C# 二进制BinaryFormatter进行序列化与反序列化
- C# 二进制BinaryFormatter进行序列化与反序列化
- BinaryFormatter序列化与反序列化(C#)
- MSDN|BinaryFormatter|序列化和反序列化|c#
- BinaryFormatter 序列化反序列化
- BinaryFormatter 序列化及反序列化
- C#WinForm - 序列化与反序列化(保存数据到文件)
- 序列化写入一个文件---反序列读取并保存
- C# 二进制读写与序列化和反序列化
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
- C# 把类实例保存到文件里(类的序列化和反序列化)
- BinaryFormatter.Deserialize 不能反序列化其它Application或者assembly产生的序列化文件
- Web Service 通过BinaryFormatter序列化和反序列化泛型List
- 二进制流序列化(反序列化)和XML序列化(反序列化)
- 序列化与反序列化(其实就是一种将各种数据转换成二进制流与二进制流的读取的概念)
- 随笔7 - 二进制序列化与反序列化
- java二进制反序列化与序列化
- PHP几个防SQL注入攻击自带函数区别
- codeforces 814B An express train to reveries(思维)
- git配置
- 安卓四大组件三之-broadcastreceiver
- 转载 经过操作 基本成功 可以跳出两个不同端口的tomcat页面
- 二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
- 友盟社会化分享 QQ空间不显示分享的图片 (已解决)
- Linux下根据进程的名字杀死进程
- Leetcode——Target Sum
- 十分钟搞定pandas(Python数据预处理库)
- 安装svn插件集成myeclipse2014的两种方式
- 从零开始写javaweb框架笔记15-搭建轻量级JAVAWEB框架-实现依赖注入功能
- 单片机Microcontrollers
- Win7系统安装软件提示"不是有效的win32应用程序"的解决方法