190 lines
5.2 KiB
C#
190 lines
5.2 KiB
C#
/********************************************************************
|
|
文件: FileTools.cs
|
|
作者: 梦语
|
|
邮箱: 1982614048@qq.com
|
|
创建时间: 2024/03/29 17:28:19
|
|
最后修改: 梦语
|
|
最后修改时间: 2024/04/03 15:59:21
|
|
功能: 文件读写工具
|
|
*********************************************************************/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
public static class FileTools
|
|
{
|
|
/// <summary>
|
|
/// 从文件中读取Json
|
|
/// </summary>
|
|
public static bool ReadFileForJson<T>(string filePath, out T data)
|
|
{
|
|
try
|
|
{
|
|
data = JsonTools.FromJson<T>(ReadFile(filePath));
|
|
return data != null;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//Debug.LogWarningFormat("文件读取错误,路径:{0}", filePath);
|
|
data = default;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
///// <summary>
|
|
///// 从文件中读取Json
|
|
///// </summary>
|
|
//public static bool ReadFileForJson(Type type, string filePath, out object data)
|
|
//{
|
|
// try
|
|
// {
|
|
// //data = JsonConvert.DeserializeObject<T>(ReadFile(filePath));
|
|
// data = JsonMapper.ToObject(ReadFile(filePath), type);
|
|
// return data != null;
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// Debug.LogWarningFormat("文件读取错误,路径:{0}", filePath);
|
|
// data = default;
|
|
// return false;
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 从文件中读取字符串
|
|
/// </summary>
|
|
public static string ReadFile(string filePath)
|
|
{
|
|
try
|
|
{
|
|
Debug.Log("当前读取路径:" + filePath);
|
|
if (!File.Exists(filePath))
|
|
{
|
|
//Debug.LogWarningFormat("该文件不存在,路径:{0}", filePath);
|
|
return null;
|
|
}
|
|
|
|
using (FileStream fs = new FileStream(filePath, FileMode.Open))
|
|
{
|
|
StreamReader reader = new StreamReader(fs);
|
|
string data = reader.ReadToEnd();
|
|
reader.Close();
|
|
fs.Close();
|
|
return data;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//Debug.LogWarningFormat("文件读取错误,路径:{0}", filePath);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 向文件中写入Json
|
|
/// </summary>
|
|
public static bool WriteFileForJson<T>(string filePath, T classData, bool isAppend = false, bool isFormat = false) where T : new()
|
|
{
|
|
string data = JsonTools.ToJson(classData, isFormat);
|
|
//string data = JsonMapper.ToJson(classData);
|
|
return WriteFile(filePath, data, isAppend);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 向文件中写入字符串
|
|
/// </summary>
|
|
public static bool WriteFile(string filePath, string data, bool isAppend = false)
|
|
{
|
|
try
|
|
{
|
|
//Debug.Log("当前写入路径:" + filePath);
|
|
FileStream fs;
|
|
|
|
if (!File.Exists(filePath))
|
|
{
|
|
string dirPath = Path.GetDirectoryName(filePath);
|
|
|
|
Directory.CreateDirectory(dirPath);
|
|
|
|
fs = new FileStream(filePath, FileMode.Create);
|
|
}
|
|
else
|
|
{
|
|
if (!isAppend)
|
|
fs = new FileStream(filePath, FileMode.Truncate);
|
|
else
|
|
fs = new FileStream(filePath, FileMode.Append);
|
|
}
|
|
|
|
StreamWriter writer = new StreamWriter(fs);
|
|
writer.Write(data);
|
|
|
|
writer.Close();
|
|
fs.Close();
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//Debug.LogErrorFormat("文件写入错误,路径:{0}", filePath);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static bool FileExists(string filePath)
|
|
{
|
|
return File.Exists(filePath);
|
|
}
|
|
|
|
public static void CopyFile(string filePath, string targetPath)
|
|
{
|
|
if (File.Exists(filePath))
|
|
{
|
|
string dirPath = Path.GetDirectoryName(targetPath);
|
|
|
|
if (!Directory.Exists(dirPath))
|
|
{
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
File.Copy(filePath, targetPath, true);
|
|
}
|
|
|
|
}
|
|
|
|
public static void FindAllFileInDirectory(string dirPath, List<string> saveList, params string[] types)
|
|
{
|
|
// 获取文件夹内所有文件和文件夹
|
|
string[] files = Directory.GetFiles(dirPath);
|
|
string[] dirs = Directory.GetDirectories(dirPath);
|
|
|
|
// 遍历文件
|
|
foreach (string file in files)
|
|
{
|
|
bool isNeed = false;
|
|
// 检查文件是否为需要的类型
|
|
foreach (string type in types)
|
|
{
|
|
if (file.EndsWith(type))
|
|
{
|
|
isNeed = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (isNeed)
|
|
{
|
|
saveList.Add(file.Replace("\\", "/"));
|
|
}
|
|
}
|
|
|
|
// 递归遍历子文件夹
|
|
foreach (string dir in dirs)
|
|
{
|
|
FindAllFileInDirectory(dir, saveList, types);
|
|
}
|
|
}
|
|
}
|