FileInfo、DirectoryInfo、File、FileStream基本使用方法以及各自的作用

时间仓促简单归纳了一下

FileInfoDirectoryInfo:用来获得文件和文件夹的属性、创建日期、修改日期、最后读取日期;以及创建、移动、重命名、判断路径是否存在等操作。

File:读写文件

FileStream:通过流来读写文件

StreamRenderStreamWriter:比较适合文本文件的操作,用于读取和写入 。

 

下边列出 FileInfo、DirectoryInfo、File、FileStream的基本属性、使用方法等

比较乱,但是写出了常用属性和方法以及他们的作用

using System.IO;
using System.Text;
using UnityEngine;

public class FileInfoTest : MonoBehaviour
{
    void Start()
    {
        #region FileInfo 文件属性 文件操作
        常用属性
        //FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "/myTest.txt");
        //Debug.Log(fileInfo.Exists);                    //判断文件是否存在
        //Debug.Log(fileInfo.Name);                  //文件名+后缀
        //Debug.Log(fileInfo.Directory);             //取得文件所在路径,路径中不带文件
        //Debug.Log(fileInfo.Length);                //获取文件数据的长度
        //Debug.Log(fileInfo.IsReadOnly);          //当前文件是否是只读的
        //Debug.Log(fileInfo.Attributes);            //获取当前文件的属性
        //Debug.Log(fileInfo.CreationTime);       //获取当前文件的创建时间
        //Debug.Log(fileInfo.CreationTimeUtc); //获取或设置当前文件或目录的协调世界时(UTC)的创建时间。
        //Debug.Log(fileInfo.DirectoryName);    //获取目录名称
        //Debug.Log(fileInfo.Extension);            //获取文件的后缀
        //Debug.Log(fileInfo.FullName);            //获取文件的完整目录,路径中带文件
        //Debug.Log(fileInfo.LastAccessTime);   //获取该文件的最后读取时间
        //Debug.Log(fileInfo.LastAccessTimeUtc);//获取该文件的协调世界时(UTC)的最后修改时间
        //Debug.Log(fileInfo.LastWriteTime);     //获取最后修改时间

        常用方法
        //FileInfo fileInfo1 = new FileInfo(Application.streamingAssetsPath + "/myTest.txt");
        //fileInfo1.Open(FileMode.Open, FileAccess.Write, FileShare.ReadWrite);          //
        //fileInfo1.OpenRead();
        //fileInfo1.OpenText();
        //fileInfo1.OpenWrite();
        //fileInfo1.AppendText();                                          //创建一个 StreamWriter,它向 FileInfo 的此实例表示的文件追加文本
        //fileInfo1.CopyTo(Application.dataPath);                //将现有文件复制到新文件,不允许覆盖现有文件。
        //fileInfo1.Delete();                                                  //删除文件,不会放回收站,直接删除。 
        //fileInfo1.Create();                                                  //创建文件。 
        //fileInfo1.CreateText();                                           // 创建写入新文本文件的 StreamWriter。 
        //fileInfo1.Decrypt();                                               //使用 Encrypt 方法解密由当前帐户加密的文件。 
        //fileInfo1.Encrypt();                                               //将某个文件加密,使得只有加密该文件的帐户才能将其解密。
        //fileInfo1.MoveTo(Application.dataPath);              //将文件移动到指定文件夹下
        //fileInfo1.Refresh();                                               //应该是刷新的意思  未测试
        //                                                                   // fileInfo1.Replace();                                              //尚未测试
        #endregion

        #region DirectoryInfo文件夹属性(目录操作)
        常用属性
        //DirectoryInfo directoryInfo = new DirectoryInfo(Application.streamingAssetsPath);
        //Debug.Log(directoryInfo.Exists);                  //判断文件夹是否存在
        //Debug.Log(directoryInfo.Name);                //文件夹的名字
        //Debug.Log(directoryInfo.Parent);               //父目录
        //Debug.Log(directoryInfo.Root);                 //根目录
        //Debug.Log(directoryInfo.CreationTime);   //创建时间

        常用方法
        //directoryInfo.CreateSubdirectory("子文件夹");  //在当前文件夹下创建子文件夹
        //directoryInfo.Create();                                       //创建文件夹
        //directoryInfo.MoveTo(Application.dataPath);    //移动文件夹到指定目录下
        //directoryInfo.Delete();                                      //删除文件夹
        //directoryInfo.GetDirectories();                          //返回该文件夹下的所有子文件夹
        //directoryInfo.GetFiles();                                    //返回该文件夹下的所有文件
        //directoryInfo.GetFileSystemInfos();                   //返回该文件夹下的所有子文件夹和文件,将其表示为一个FileSystemInfo引用数组

        很多方法与字面意思相同
        #endregion

        #region 使用File类来读写文件 一般用于读取文本文件
        //读取文本文件常用的三种方法
        //string[] line = File.ReadAllLines(Application.streamingAssetsPath + "/myTest.txt");//读取文本文件中的所有行,并返回一个string数组,
        //string allText = File.ReadAllText(Application.streamingAssetsPath + "/myTest.txt");     //将文件读取为一个string字符串
        //byte[] data = File.ReadAllBytes(Application.streamingAssetsPath + "/TestPecture.jpg");     //将文件读取为Byte数组
        //File.WriteAllBytes(Application.streamingAssetsPath + "/MyPecture.png", data);      //写入byte
        //File.WriteAllLines(Application.streamingAssetsPath + "/myTest.txt", line);               //以行的形式写入 字符串数组
        //File.WriteAllText(Application.streamingAssetsPath + "/myTest.txt", allText);            //写入字符串
        #endregion

        #region 使用FileStream读取文件  
        //第一步:创建文件流,用来操作文件
        // FileStream fileStream = new FileStream(Application.streamingAssetsPath + "/myTest.txt", FileMode.Open,FileAccess.Read);
        //FileMode打开文件的几种方式
        //FileMode.Append;          //若文件存在,则找到文件并找到文件结尾,或者创建一个新文件。
        //FileMode.Create;             //指定操作系统创建新文件,如果文件已存在则覆盖。
        //FileMode.CreateNew;        //指定操作系统应创建新文件,如果文件存在则引发异常。
        //FileMode.Open;                //指定 操作系统应打开现有文件,如果文件不存在则抛出异常。
        //FileMode.OpenOrCreate;  //如果文件存在就打开,如果不存在就创建
        //FileMode.Truncate;           //定操作系统打开现有文件,如果文件已存在则清空,从Truncate打开的文件中读取将引发异常。       

        //第二步:读取或者写入文件
        读取文件:
        //FileStream readStream = new FileStream(Application.streamingAssetsPath + "/myTest.txt", FileMode.Open, FileAccess.Read);
        //byte[] data = new byte[readStream.Length];
        //readStream.Read(data, 0, data.Length);
        //string ma = Encoding.UTF8.GetString(data, 0, data.Length);
        //readStream.Close();

        写入文件 比如写入以上的data
        //FileStream writeStream = new FileStream(Application.streamingAssetsPath + "/testTest.txt", FileMode.Create, FileAccess.Write);
        //writeStream.Write(data,0, data.Length);
        //writeStream.Close();
        #endregion

        #region StreamReader 和 StreamWriter读取和写入文本文件
        //StreamReader会帮我们处理编码格式,所以我们不用关心文本文件的编码格式是什么
        StreamReader streamReader;
        //StreamReader创建文本文件读取流
        streamReader = new StreamReader(Application.streamingAssetsPath + "/myTest.txt");
        //或者我们可以指定编码格式
        streamReader = new StreamReader(Application.streamingAssetsPath + "/myTest.txt", Encoding.UTF8);
        //在文件流的基础上创建文本读取流
        FileStream fileInfo = new FileStream(Application.streamingAssetsPath + "/myTest.txt",FileMode.Open,FileAccess.Read);
        streamReader = new StreamReader(fileInfo);

        //文本文件读取读取
        string read = streamReader.ReadLine();                //读取一行
        read = streamReader.ReadToEnd();                      //读取到最后一行,文本的结尾,读取所有的字符
        char readinfo = (char)streamReader.Read();         //读取一个字符,一个字一个字的读,没有读到的话返回-1

        //文本文件写入流写入
        Debug.Log(read);
        StreamWriter writer=new StreamWriter(Application.streamingAssetsPath + "/myTest1.txt");//如果当前文件夹下没有文件,则创建文件,如果有文件则会清空文件
        writer.Write(read);            //写入字符串
        writer.WriteLine(read);     //写入一行
        writer.Close();//写入完成后记得关闭写入流
        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值