OI基础操作

----------------------------------IO------------------------------
Directory.Exists(path) //判断文件夹是否存在
------------------------------------------------------------------
Directory.CreateDirectory(path) // 根据路径创建出文件夹
------------------------------------------------------------------
File.Exists(path) //判断文件是否存在
------------------------------------------------------------------
Path.Combine(path, info.text) //路径拼接
------------------------------------------------------------------
DirectoryInfo directoryInfo = new DirectoryInfo(path) //创建文件夹操作对象
directoryInfo.FullName //获取文件夹名
directoryInfo.CreationTime //获取创建时间
------------------------------------------------------------------
FileInfo fileInfo = new FileInfo(path) //创建文件操作对象
fileInfo.FullName // 获取文件名 
fileInfo.CreationTime // 获取创建时间
------------------------------------------------------------------
DirectoryInfo directory = DirectroyInfo.CreateDirectory(path) //创建文件夹
------------------------------------------------------------------
Directory.Move(path,path2) //移动文件夹到path2
------------------------------------------------------------------
Directory.Delete(path) //删除文件夹
------------------------------------------------------------------
File.Create(path) // 创建文件
------------------------------------------------------------------
File.Copy(path,path2) //复制文件到path2
File.Move(path,path2) //移动文件到path2
File.Delete(path) //删除文件
------------------------------------------------------------------
using(FileStream fileStream = File.Create(path)) //打开文件流(创建并写入数据)
{
    StreamWriter sw = new StreamWriter(fileStream)
    sw.WriteLine("12345678") // 写入12345678
    sw.Flush()
}
------------------------------------------------------------------
using(StreamWirter sw = File.AppendText(path)) //流,写入器(创建/打开文件并写入)
{
    string msg = "大家好,早上好!";
    sw.WriteLine(msg);
    sw.Flush();
}
---------------------------不建议做多线程读取,如果要做,要加锁,反多线程-------------------------------
---------------------------读取第一种---------------------------------------
foreach(string res in File.ReadAllLines(path)) //读取文件中所有的行信息
{
    console.WriteLine(res)
}
---------------------------读取第二种---------------------------------------
string res = File.ReadAllText(path)
---------------------------读取第三种---------------------------------------
 Byte [] byte = File.ReadAllBytes(path);
string res System.Text.Encoding.UTF8.GetString(byte);
---------------------------读取第四种,超大文件分批读取---------------------------------------
using(FileStream fs = File.OpenRead(path))
{
    int length = 5;
    int res = 0;
    do{
        byte[] bytes = new byte[length];
        res = fs.Read(bytes,0,5);
        for(int i=0;i<res;i++)
        {
            Console.WriteLine(bytes[i].ToString());
        }
    }
    while(length == res)
}

 

posted @ 2024-08-23 14:38  龙卷风吹毁停车场  阅读(26)  评论(0)    收藏  举报