unity文本简单的读写

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace SDGB
{

/// <summary>
///文件帮助类
/// </summary>
public class FileTools
{
    /// <summary>
    /// 文本的读取
    /// </summary>
    /// <param name="path"></param>
    public static List<string> ReadFile(string path)
    {

        if (!string.IsNullOrEmpty(path) && path != null)
        {

            string line = null;
            List<string> lines = new List<string>();
            using (StreamReader streamReader = new StreamReader(path, new UTF8Encoding(false)))
            {

                while ((line = streamReader.ReadLine()) != null)
                {

                    lines.Add(line);
                }
            }
            return lines;
        }
        return null;
    }

    /// <summary>
    /// 读取文本,得到是一段字符串
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string GetJsonString(string path)     //从文件里面读取json数据
    {
        //读取Json数据
        StreamReader reader = new StreamReader(path);
        string jsonData = reader.ReadToEnd();
        reader.Close();
        reader.Dispose();
        return jsonData;

    }

    /// <summary>
    /// 文本清空重新写入数据
    /// </summary>
    /// <param name="path"></param>
    /// <param name="data"></param>
    public static void WriteFile(string path, string[] data)
    {
        if (!string.IsNullOrEmpty(path) && path != null)
        {
            using (StreamWriter streamWriter = new StreamWriter(path, false, new UTF8Encoding(false)))
            {
                int index = 0;
                while (index < data.Length)
                {
                    streamWriter.WriteLine(data[index]);
                    index++;
                }
            }
        }
    }

    /// <summary>
    /// 不清楚文本,在原来的基础上叠加写入数据
    /// </summary>
    /// <param name="path"></param>
    /// <param name="data"></param>
    public static void WriteFile(string path, string data)
    {

        if (!string.IsNullOrEmpty(path) && path != null)
        {
            StreamWriter writer = null;

            FileInfo file = new FileInfo(path);
            if (!file.Exists)
            {
                writer = file.CreateText();
            }
            else
            {
                writer = file.AppendText();
            }

            writer.WriteLine(data);
            writer.Flush();
            writer.Dispose();
            writer.Close();


        }

    }






    /// <summary>
    /// 按顺序加载
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string[] GetAllSortPaths(string path)
    {

        List<string> pathArray = new List<string>();

        if (!string.IsNullOrEmpty(path))
        {

            try
            {

                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                FileInfo[] infos = directoryInfo.GetFiles();

                Array.Sort(infos, (x1, x2) => int.Parse(Regex.Match(x1.Name, @"\d+").Value).CompareTo(int.Parse(Regex.Match(x2.Name, @"\d+").Value)));

                for (int i = 0; i < infos.Length; i++)
                {
                    if (infos[i].ToString().EndsWith("meta") || infos[i].ToString().EndsWith("dll"))
                        continue;
                    pathArray.Add(infos[i].ToString());
                }


            }
            catch (System.Exception e)
            {

                throw new System.Exception(e.ToString());
            }

            return pathArray.ToArray();

        }

        return null;


    }






    /// <summary>	
    /// /// 文件编码转换	
    /// /// </summary>	
    /// /// <param name="sourceFile">源文件</param>	
    /// /// <param name="destFile">目标文件,如果为空,则覆盖源文件</param>	
    /// /// <param name="targetEncoding">目标编码</param>	
    public static void ConvertFileEncoding(string sourceFile, string destFile, Encoding targetEncoding)
    {
        destFile = string.IsNullOrEmpty(destFile) ? sourceFile : destFile;

        File.WriteAllText(destFile, File.ReadAllText(sourceFile, Encoding.UTF8), targetEncoding);

    }












}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值