C#处理文件

发布于 2020-04-03  40 次阅读


前言

今天被朋友问到这方面的问题,莫名的发现自己明明做过很多,而且还封装了一些,但是却不能在最快的时间回忆起来解决问题,给自己捏脸差评。

正文

加载有好几种
平台的路径这个之前说过了

Resources

Resources是Unity内置的加载文件,发布的时候会把入包里。

using System.Collections; 
using System.IO;
using UnityEngine;

public class ResourceHelper
{
    //Hashtable用来管理我们加载出来的资源的,作用就跟字典差不多
    private static Hashtable hashTexts = new Hashtable();
    private static Hashtable hashImages = new Hashtable();
    private static Hashtable hashPrefabs = new Hashtable();

    #region 加载资源
    /// <summary>
    /// 加载Prefab
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static GameObject LoadPrefab(string path)
    {
        return LoadPfb(path);
    }
    /// <summary>
    /// 加载文本
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string LoadText(string path)
    {
        return LoadTextFile(path, ".txt");
    }
    /// <summary>
    /// 加载XML文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string LoadXML(string path)
    {
        return LoadTextFile(path, ".xml");
    }
    /// <summary>
    /// 加载图片文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static Texture2D LoadTexture(string path)
    {
        return LoadTextrue(path);
    }
    public static Sprite LoadSprite(string path)
    {
        return LoadSpr(path);
    }
    #endregion
    private static string LoadTextFile(string path, string ext)
    {
        return LoadTextF(path, ext);
    }
    /// <summary>
    /// 加载预设体
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    private static GameObject LoadPfb(string path)
    {
        object obj = hashPrefabs[path];
        if (obj == null)
        {
            hashPrefabs.Remove(path);
            GameObject gameObject = Resources.Load(path) as GameObject;
            hashPrefabs.Add(path, gameObject);
            return gameObject;
        }

        return obj as GameObject;
    }

    /// <summary>
    /// 加载文本文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="ext"></param>
    /// <returns></returns>
    private static string LoadTextF(string path, string ext)
    {
        object obj = hashTexts[path];
        if (obj == null)
        {
            hashTexts.Remove(path);
            string text = string.Empty;

#if UNITY_EDITOR || UNITY_STANDALONE_WIN
            string pathstr = Application.dataPath + "/StreamingAssets/" + path + ext;
#else
             string pathstr = Application.persistentDataPath + path + ext;
#endif

            text = File.ReadAllText(pathstr);

            hashTexts.Add(pathstr, ext);
            return text;
        }
        return obj as string;
    }

    /// <summary>
    /// 加载图片
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    private static Texture2D LoadTextrue(string path)
    {
        object obj = hashImages[path];
        if (obj == null)
        {
            hashImages.Remove(path);
            Texture2D texture2D = (Texture2D)Resources.Load(path, typeof(Texture2D));
            hashImages.Add(path, texture2D);
            return texture2D;
        }
        return obj as Texture2D;
    }
    /// <summary>
    /// 加载精灵图片
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    private static Sprite LoadSpr(string path)
    {
        object obj = hashImages[path];
        if (obj == null)
        {
            hashImages.Remove(path);
            Sprite sprite = (Sprite)Resources.Load(path, typeof(Sprite));
            hashImages.Add(path, sprite);
            return sprite;
        }
        return obj as Sprite;
    }
}

WWW

www加载文件也是以前经常用,就不多讲了

WWW www = new WWW("file:///路径");
yield return www;
if (www != null && string.IsNullOrEmpty(www.error))
{
    //做处理

}

处理文件

这里用到IO File Directory。

using System;
using System.IO; 

public class DirFileHelper
{
    #region 文件夹部分 
    /// <summary>
    /// 生成任何类型的文件
    /// </summary>
    /// <param name="fileBytes">文件流</param>
    /// <param name="path">当前文件的路径</param>
    /// <param name="folderName">文件夹</param>
    /// <param name="fileName">文件名带后缀</param>
    public static void SavaFile(byte[] fileBytes, string path, string folderName, string fileName)
    {
        FileStream fs = new FileStream(path + "/" + folderName + "/" + fileName, FileMode.Create, FileAccess.Write);
        fs.Write(fileBytes, 0, fileBytes.Length);
        fs.Dispose();
        fs.Close();
    }
    public static void SavaFile(byte[] fileBytes, string path, string fileName)
    {
        FileStream fs = new FileStream(path + "/" + fileName, FileMode.Create, FileAccess.Write);
        fs.Write(fileBytes, 0, fileBytes.Length);
        fs.Dispose();
        fs.Close();
    }
    public static void SavaFile(byte[] fileBytes, string filepath)
    {
        using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write))
        {
            fs.Write(fileBytes, 0, fileBytes.Length);
            fs.Dispose();
            fs.Close();
        }
    }
    /// <summary>
    /// 去读文件
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static byte[] ReadFile(string filePath)
    {
        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            int dataLength = (int)fs.Length; //获得每个文件的大小
            byte[] buffer = new byte[dataLength];
            fs.Read(buffer, 0, buffer.Length);
            return buffer;
        }
     }

    /// <summary>
    /// 创建一个文本文件
    /// </summary>
    /// <param name="fileName">文件路径</param>
    /// <param name="content">文件内容</param>
    public static void CreateFileTxt(string fileName, string content)
    {
        StreamWriter streamWriter = File.CreateText(fileName);
        streamWriter.Write(content);
        streamWriter.Close();
    }
    /// <summary>
    /// 创建一个文件夹
    /// </summary>
    /// <param name="fileName">这个fileName 是 path + id </param>
    public static void CreateDirectory(string fileName)
    {
        if (!IsExistsDirectory(fileName))
        {
            Directory.CreateDirectory(fileName);
        }
    }
    /// <summary>
    /// 删除一个文件夹
    /// </summary>
    /// <param name="fileName"></param>
    public static void DeleteDirectory(string fileName)
    {
        if (IsExistsDirectory(fileName))
        {
            Directory.Delete(fileName, true);
        }
    }
    /// <summary>
    /// 获得当前文件夹下所有文件夹
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string[] GetAllDirectory(string path)
    {
        return Directory.GetDirectories(path);
    }
    /// <summary>
    /// 获取路径下所有的文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string[] GetAllFile(string path)
    {
        return Directory.GetFiles(path);
    }
    /// <summary>
    /// 获取路径下某类文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="searchPattern">*jpg</param>
    /// <returns></returns>
    public static string[] GetAllFile(string path, string searchPattern)
    {
        return Directory.GetFiles(path, searchPattern);
    }
    /// <summary>
    /// 获取系统时间
    /// </summary>
    /// <returns></returns> 
    public static string GetCurrentTime(string mat)
    {
        return DateTime.Now.ToString("yyyy" + mat + "MM" + mat + "dd" + mat + "HH" + mat + "mm" + mat + "ss");
    }
    /// <summary>
    /// 判断文件是否存在
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static bool IsExistsFile(string fileName)
    {
        return File.Exists(fileName);
    }
    /// <summary>
    /// 判断文件夹是否存在
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static bool IsExistsDirectory(string fileName)
    {
        return Directory.Exists(fileName);
    }
    #endregion

}

就到这吧,下次有机会再说其他


不积跬步,无以至千里;不积小流,无以成江海。