89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
namespace Adam
|
|
{
|
|
|
|
public class FileUtil
|
|
{
|
|
public static void WriteToLocal(string data, string fileName)
|
|
{
|
|
string path = GetPath(fileName);
|
|
if (!File.Exists(path))
|
|
{
|
|
File.Create(path).Close();
|
|
}
|
|
File.WriteAllText(path, data, Encoding.UTF8);
|
|
}
|
|
|
|
public static string ReadFromLocal(string fileName)
|
|
{
|
|
string path = GetPath(fileName);
|
|
if (!File.Exists(path)) return "请选择场景";
|
|
string data = File.ReadAllText(path);
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回指定行的数据
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
public static string ReadFromLocal(string fileName, int index)
|
|
{
|
|
string path = GetPath(fileName);
|
|
if (!File.Exists(path)) return "请选择场景";
|
|
string[] data = File.ReadAllLines(path);
|
|
return data[index];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回所有行的数据
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
public static string[] ReadAllLineFromLocal(string fileName)
|
|
{
|
|
string path = GetPath(fileName);
|
|
if (!File.Exists(path)) return null;
|
|
string[] data = File.ReadAllLines(path);
|
|
return data;
|
|
}
|
|
|
|
public static string Split(string data, int index)
|
|
{
|
|
string[] s = data.Split(',');
|
|
if (index > s.Length)
|
|
return "科目不存在";
|
|
else
|
|
return s[index];
|
|
}
|
|
/// <summary>
|
|
/// 路径
|
|
/// </summary>
|
|
/// <param name="fileName">数据文件</param>
|
|
/// <returns></returns>
|
|
public static string GetPath(string fileName)
|
|
{
|
|
string path = Application.dataPath + "/StreamingAssets/" + fileName;
|
|
return path;
|
|
}
|
|
/// <summary>
|
|
/// 路径
|
|
/// </summary>
|
|
/// <param name="file">子文件夹</param>
|
|
/// <param name="fileName">数据文件</param>
|
|
/// <returns></returns>
|
|
public static string GetPath(string file, string fileName)
|
|
{
|
|
string path = Application.dataPath + "/StreamingAssets/" + file + "/" + fileName;
|
|
return path;
|
|
}
|
|
}
|
|
|
|
} |