File类的使用

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

public class Utils_File
{
    Utils_File() { }
    private static Utils_File _instance;
    public static Utils_File instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Utils_File();
            }
            return _instance;
        }
    }

    #region File
    public void File_Create(string path)
    {
        if (!File.Exists(path))
        {
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            fs.Dispose();
        }
    }

    public string File_Read(string path)
    {
        string content = string.Empty;
        if (File.Exists(path))
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            content = sr.ReadToEnd();

            sr.Dispose();
            fs.Dispose();
        }
        return content;
    }

    public List<string> File_Read_Line(string path)
    {
        List<string> contents = new List<string>();
        if (File.Exists(path))
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            string content = string.Empty;
            while (sr.Peek() != -1)
            {
                content = sr.ReadLine();
                contents.Add(content);
            }

            sr.Dispose();
            fs.Dispose();
        }
        return contents;
    }

    public void File_Write(string path, string content, bool append = false, bool newLine = false)
    {
        FileMode mode = FileMode.OpenOrCreate;
        if (File.Exists(path) && append)
        {
            mode = FileMode.Append;
        }

        FileStream fs = new FileStream(path, mode);
        StreamWriter sw = new StreamWriter(fs, Encoding.Default);

        sw.Write(content);
        if (newLine)
        {
            sw.WriteLine("");
        }

        sw.Dispose();
        fs.Dispose();
    }

    public void File_Write_Line(string path, List<string> contents, bool append = false)
    {
        FileMode mode = FileMode.OpenOrCreate;
        if (File.Exists(path) && append)
        {
            mode = FileMode.Append;
        }

        FileStream fs = new FileStream(path, mode);
        StreamWriter sw = new StreamWriter(fs, Encoding.Default);

        for (int i = 0; i < contents.Count; i++)
        {
            sw.WriteLine(contents[i]);
        }
        
        sw.Dispose();
        fs.Dispose();
    }

    public void File_Delete(string path)
    {
        if (File.Exists(path))
        {
            File.Delete(path);
        }
    }

    public void File_Copy(string path_o, string path_n)
    {
        if (File.Exists(path_o))
        {
            File_Delete(path_n);
            File.Copy(path_o, path_n, true);
        }
    }

    public void File_Move(string path_o, string path_n)
    {
        if (File.Exists(path_o))
        {
            File.Move(path_o, path_n);
        }
    }

    public bool File_Contain(string path)
    {
        return File.Exists(path);
    }

    public void File_Find(string path)
    {
        DirectoryInfo dicInfo = new DirectoryInfo(path);
        FileInfo[] _files = dicInfo.GetFiles();
        List<FileInfo> files = _files.ToList<FileInfo>();

        foreach (FileInfo file in files)
        {
            UnityEngine.Debug.LogError(file.Name);
        }
    }
    #endregion

    #region Directory
    public void Dic_Create(string path)
    {
        if (!Directory.Exists(path))
        {
            DirectoryInfo dic = Directory.CreateDirectory(path);
        }
    }

    public void Dic_Delete(string path)
    {
        if (Directory.Exists(path))
        {
            Directory.Delete(path);
        }
    }

    public void Dic_Move(string path_o,string path_n)
    {
        if (Directory.Exists(path_o))
        {
            Directory.Move(path_o, path_n);
        }
    }

    public bool Dic_Contain(string path)
    {
        return Directory.Exists(path);
    }

    public void Dic_Find(string path)
    {
        string[] dirs = Directory.GetDirectories(path);
        foreach (string dir in dirs)
        {
            UnityEngine.Debug.LogError(dir);
        }
    }
    #endregion
}
View Code

文件:建立,读取,写入,复制,移动,删除,目录下全部文件app

文件夹:建立,移动,删除,目录下全部文件夹ide

 

功能注释:this

  1.peek 是用来肯定你read的文件是否结束了,若是结束了会返回int型 -1 spa

  2.flush 使用此方法将全部信息从基础缓冲区移动到其目标或清除缓冲区,或者同时执行这两种操做(没使用)code

  3.FileStream类的Close()方法是继承于Stream类的,源代码是这样的对象

    public virtual void Close() 
    {
      Dispose(true); 
      GC.SuppressFinalize(this);
    }
    FileStream类的Dispose()方法是继承于Stream类的,源代码是这样的:
    public void Dispose() 
    {
      Close(); 
    }
 blog

  是一个标准的Dispose模式的实现,Close()方法调用的是带参数的Dispose方法,而后调用GC.SuppressFinalize (this);请求系统不要调用指定对象的终结器。而Dispose()方法直接调用Close()方法!继承

  对于FileStream类来讲,Close()方法和Dispose()方法是没有区别!接口

  4.using语句提供了一个脉络清晰的机制来控制资源的生存期,建立的对象会在using语句结束时被摧毁,使用前提该对象必须继承了IDisposable接口。资源

相关文章
相关标签/搜索