#region XML
/// <summary>
/// 保存 XML
/// </summary>
/// <param name="filePath">保存文件路径</param>
/// <param name="source">内容</param>
/// <param name="type">内容类型</param>
/// <param name="rootName">根节点名称</param>
public static bool SaveToXML(string filePath,object source,Type type, string rootName = null)
{
if(string.IsNullOrWhiteSpace(filePath))
{
LogUtil.Log("filePath is null");
return false;
}
if(source == null)
{
LogUtil.LogError("source is not allowed to be null");
return false;
}
if(type == null)
{
type = source.GetType();
}
//文件夹判断
string dirPath = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
using(StreamWriter writer = new StreamWriter(filePath))
{
try
{
XmlSerializer xml = string.IsNullOrWhiteSpace(rootName)
? new XmlSerializer(type):new XmlSerializer(type, new XmlRootAttribute(rootName));
xml.Serialize(writer, source);
}
catch(System.Exception e)
{
LogUtil.LogError(e.Message);
return false;
}
}
LogUtil.Log("XML Save Success!");
return true;
}
/// <summary>
/// 加载 XML
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="type">类型</param>
/// <param name="rootName">根节点名称</param>
/// <returns></returns>
public static object LoadFromXML(string filePath,Type type,string rootName)
{
if(!File.Exists(filePath))
{
LogUtil.LogError("filePath is not exit");
return null;
}
if(type == null)
{
LogUtil.LogError("type is not allowed to be null");
return null;
}
object obj = null;
try
{
using(StreamReader stream = new StreamReader(filePath))
{
XmlSerializer xml = string.IsNullOrWhiteSpace(rootName)
? new XmlSerializer(type) : new XmlSerializer(type, new XmlRootAttribute(rootName));
obj = xml.Deserialize(stream);
}
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return null;
}
LogUtil.Log("Load From XML Successful~");
return obj;
}
#endregion
#region 二进制
/// <summary>
/// 保存 二进制
/// </summary>
/// <param name="filePath">文件保存路径</param>
/// <param name="source">内容</param>
/// <param name="Hidden">文件夹是否隐藏</param>
/// <returns></returns>
public static bool SaveToBinary(string filePath,object source,bool Hidden)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.Log("filePath is null");
return false;
}
if (source == null)
{
LogUtil.LogError("source is not allowed to be null");
return false;
}
try
{
using(FileStream stream = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.Write,FileShare.Write))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, source);
}
}
catch(Exception x)
{
LogUtil.LogError(x.Message);
return false;
}
finally
{
File.SetAttributes(filePath, Hidden ? FileAttributes.Hidden : FileAttributes.Normal);
}
LogUtil.Log("Binary Save Success~");
return true;
}
/// <summary>
/// 加载 二进制
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static object LoadFromBinary(string filePath)
{
if (!File.Exists(filePath))
{
LogUtil.LogError("filePath is not exit");
return null;
}
object obj = null;
try
{
using(FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BinaryFormatter bf = new BinaryFormatter();
obj = bf.Deserialize(stream);
}
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return null;
}
LogUtil.Log("Load From Binary Successful~");
return obj;
}
#endregion
#region MyRegion
/// <summary>
/// 保存 Json (静态字段无法保存)
/// </summary>
/// <param name="filePath">保存文件路径</param>
/// <param name="source">内容</param>
/// <returns></returns>
public static bool SaveToJson_1(string filePath,object source)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.Log("filePath is null");
return false;
}
if (source == null)
{
LogUtil.LogError("source is not allowed to be null");
return false;
}
try
{
File.WriteAllText(filePath, JsonConvert.SerializeObject(source));
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return false;
}
LogUtil.Log("Json Save Success~");
return true;
}
/// <summary>
/// 保存 Json (中文是乱码。不可以有float。不能有二维数组。加载正常)
/// </summary>
/// <param name="filePath">保存文件路径</param>
/// <param name="source">内容</param>
/// <returns></returns>
public static bool SaveToJson_2(string filePath, object source)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.Log("filePath is null");
return false;
}
if (source == null)
{
LogUtil.LogError("source is not allowed to be null");
return false;
}
try
{
File.WriteAllText(filePath, JsonMapper.ToJson(source));
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return false;
}
LogUtil.Log("Json Save Success~");
return true;
}
/// <summary>
/// 保存 Json (静态字段无法保存,带有泛型的字段可能存在问题,还有其他等。大工程不建议)
/// </summary>
/// <param name="filePath">保存文件路径</param>
/// <param name="source">内容</param>
/// <returns></returns>
public static bool SaveToJson_3(string filePath, object source)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.Log("filePath is null");
return false;
}
if (source == null)
{
LogUtil.LogError("source is not allowed to be null");
return false;
}
try
{
File.WriteAllText(filePath, JsonUtility.ToJson(source));
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return false;
}
LogUtil.Log("Json Save Success~");
return true;
}
/// <summary>
/// 读取 Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static T LoadFromJson_1<T>(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.LogError("filePath is not allowed to be null");
return default(T);
}
if(!File.Exists(filePath))
{
LogUtil.LogError("filePath is not exit");
return default(T);
}
T t = default(T);
try
{
string json = File.ReadAllText(filePath);
//using (StringReader sr = new StringReader(json))
//{
// JsonSerializer serializer = new JsonSerializer();
// t = serializer.Deserialize<T>(new JsonTextReader(sr));
//}
t = JsonConvert.DeserializeObject<T>(json);
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return default(T);
}
LogUtil.Log("Load From Json Successful");
return t;
}
/// <summary>
/// 读取 Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static T LoadFromJson_2<T>(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.LogError("filePath is not allowed to be null");
return default(T);
}
if (!File.Exists(filePath))
{
LogUtil.LogError("filePath is not exit");
return default(T);
}
T t = default(T);
try
{
string json = File.ReadAllText(filePath);
t = JsonMapper.ToObject<T>(json);
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return default(T);
}
LogUtil.Log("Load From Json Successful");
return t;
}
/// <summary>
/// 读取 json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static T LoadFromJson_3<T>(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
LogUtil.LogError("filePath is not allowed to be null");
return default(T);
}
if (!File.Exists(filePath))
{
LogUtil.LogError("filePath is not exit");
return default(T);
}
T t = default(T);
try
{
string json = File.ReadAllText(filePath);
t = JsonUtility.FromJson<T>(json);
}
catch(Exception e)
{
LogUtil.LogError(e.Message);
return default(T);
}
LogUtil.Log("Load From Json Successful");
return t;
}
#endregion
保存加载(XML,二进制,JSON)
最新推荐文章于 2022-08-30 19:31:12 发布