zoukankan      html  css  js  c++  java
  • Unity中删除文件目录下的所有文件和查看文件里面的内容

    这个脚本中存储了:

    删除文件夹中所有的文件的方法,

    删除单个文件的方法

    获取文本内容的方法

    获取其他类型文本内容的方法

    写入文本文件的方法

    void Start () {
        string filePath = Application.streamingAssetsPath + "/" + "abb.txt";
     
        #region 进行文件的删除
        //string fullPath = Application.streamingAssetsPath;
        //bool isTrue = DeleteAllFile(fullPath);
        //if (isTrue)
        //{
        //    Debug.Log("删除成功!!!!!!");
        //}
        //else
        //{
        //    Debug.Log("删除失败!!!!!!");
        //}
        #endregion
     
        #region 进行文件内容的获取的第一种方式
        //string m_Str = GainFileContent_1(filePath);
        //Debug.Log(m_Str);
        #endregion
     
        #region 进行文件内容的获取的第二种方式
        //string m_Str = GainFileContent_2(filePath);
        //Debug.Log(m_Str);
        #endregion
     
        #region 进行文件内容的获取的第三种方式
        string m_Str = GainFileContent_3(filePath);
        Debug.Log(m_Str);
        #endregion
     
        #region 进行文件内容的写入
        bool isTrue = WriteFileContent_3(filePath);
        if (isTrue)
        {
            Debug.Log("写入成功!!!!!!");
        }
        else
        {
            Debug.Log("写入失败!!!!!!");
        }
        #endregion
    }
    /// <summary>
    /// 删除指定文件目录下的所有文件
    /// </summary>
    /// <param name="fullPath">文件路径</param>
    public bool DeleteAllFile(string fullPath)
    {
        //获取指定路径下面的所有资源文件  然后进行删除
        if (Directory.Exists(fullPath))
        {
            DirectoryInfo direction = new DirectoryInfo(fullPath);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
     
            Debug.Log(files.Length);
     
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".meta"))
                {
                    continue;
                }
                string FilePath = fullPath + "/" + files[i].Name;
                print(FilePath);
                File.Delete(FilePath);
            }
            return true;
        }
        return false;
    }
     
    /// <summary>
    /// 第一种:获取指定文件中的内容
    /// </summary>
    public string GainFileContent_1(string filePath)
    {
        string m_Str = null;
        try
        {
            //读取文件的所有行,并将数据读取到定义好的字符数组strs中,一行存一个单元
            string[] strs = File.ReadAllLines(filePath);
            for (int i = 0; i < strs.Length; i++)
            {
     
                m_Str += strs[i];//读取每一行,并连起来
                m_Str += "
    ";//每一行末尾换行
            }
            return m_Str;
        }
        catch (System.Exception e)
        {
            Debug.Log(e.Message);
            return m_Str;
        }
         
    }
     
    /// <summary>
    /// 从一个文本文档或非txt文本文档中获取内容
    /// </summary>
    /// <param name="m_FileName">文件的路径和名字</param>
    public string GainFileContent_2(string m_FileName)
    {
        try
        {
            string pathSource = m_FileName;
            using (FileStream fsSource = new FileStream(pathSource,
                        FileMode.Open, FileAccess.Read))
            {
                byte[] bytes = new byte[fsSource.Length];
                int numBytesToRead = (int)fsSource.Length;
                int numBytesRead = 0;
                while (numBytesToRead > 0)
                {
                    int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
                    if (n == 0)
                        break;
                    numBytesRead += n;
                    numBytesToRead -= n;
                }
                numBytesToRead = bytes.Length;
                string m_Str = UTF8Encoding.UTF8.GetString(bytes);
                return m_Str;
            }
        }
        catch(System.Exception e)
        {
            Debug.Log(e.Message);
            return null;
        }
    }
     
    /// <summary>
    /// 进行一个文本的读取内容
    /// </summary>
    /// <param name="FilePath"></param>
    /// <returns></returns>
    public string GainFileContent_3(string FilePath)
    {
        // ReadAllText方法第一个参数是要读取txt文件的路径,第二个参数是编码方式,这里采用默认
        string m_Str = File.ReadAllText(FilePath, Encoding.Default);
        return m_Str;
    }
     
    /// <summary>
    /// 将一个文件写入到文本当中
    /// </summary>
    /// <param name="FilePath"></param>
    /// <returns></returns>
    public bool WriteFileContent_3(string FilePath)
    {
        try
        {
            File.AppendAllText(FilePath, "我被写进来了", Encoding.Default);
            return true;
        }
        catch (System.Exception e)
        {
            Debug.Log(e.Message);
            return false;
        }
        
    }

    以上实现了对于文本文件的增,删,查,希望能帮助到大家!!!!!!

  • 相关阅读:
    Mybatis专栏文章整理成册《Mybatis进阶》!!!
    Mybatis的几种传参方式,你了解吗?
    HDU 1890
    POJ 2186
    HDU 2896
    POJ 1322
    POJ 1276
    POJ 1208
    POJ 1189
    POJ 1178
  • 原文地址:https://www.cnblogs.com/baosong/p/9589112.html
Copyright © 2011-2022 走看看