string str = @"F:视频C#全套视频教程视频教程之10-面向对象多态(第十二天)video1、复习.avi";//获取文件名string str1 = Path.GetFileName(str);Console.WriteLine(str1);//获取文件名 不包含扩展名string str2 = Path.GetFileNameWithoutExtension(str1);Console.WriteLine(str2);//获取文件扩展名string str3 = Path.GetExtension(str1);Console.WriteLine(str3);// 获取文件所在的目录string str4 = Path.GetDirectoryName(str);Console.WriteLine(str4);// 获取文件的绝对路径string str5 = Path.GetFullPath(str);Console.WriteLine(str5);// 路径组合string str6 = Path.Combine(@"1", @"2");Console.WriteLine(str6);
//F:xue_//创建文件File.Create(@"F:xue_");// 删除文件File.Delete(@"F:xue_");string path = @"F:xue_";string path1 = @"F:xue_";//复制(原文件、新文件)File.Copy(path, path1);//文件读写(一)//读取string path = @"F:xue_";byte[] buffer = File.ReadAllBytes(path);// 指定编码格式: Unicode(UTF-8 + UTF-7 + UTF-32) GB2312是简体中文 GBK(简体中文+繁体中文)// 常用:UTF-8 GB2312 ASCII GBK // GetString 是转为字符串,GetEncoding(GB2312 ASCII GBK )//string str = Encoding.GetEncoding("GB3212").GetString(buffer);//string str = Encoding.GetEncoding("GBK").GetString(buffer);//string str = Encoding.GetEncoding("ASCII").GetString(buffer);string str = Encoding.UTF8.GetString(buffer);Console.WriteLine(str);//写入string path = @"F:xue_";string str = "今天真不错";// 字符串 转 字节数组byte[] buffer = Encoding.Default.GetBytes(str);File.WriteAllBytes(path, buffer);Console.WriteLine("写入成功");//文件读写(二)//按行读取string path = @"F:xue_";string[] contents = File.ReadAllLines(path, Encoding.Default);foreach (string line in contents){Console.WriteLine(line);}// 全读取 string path = @"F:xue_";string str = File.ReadAllText(path, Encoding.Default);Console.WriteLine(str);// 行方式 写入string path = @"F:xue_";File.WriteAllLines(path, new string[] { "对对对", "是是是", "你说的都对 " });Console.WriteLine("已经写入");// 全写入string path = @"F:xue_";File.WriteAllText(path, "对对对对对对对对对对对对对对对对对对对对对,是是是是是是是是是");Console.WriteLine("已经写入");// 追加string path = @"F:xue_";File.AppendAllText(path, "恩恩恩恩恩恩恩恩恩恩恩恩恩");
// 文件流读取// 路径、操作(追加、打开 若无 则创建、)、权限r w 、// FileMode.Append 追加// FileMode.OpenOrCreate 打开 若无 则创建string path = @"F:xue_x";// 读取 Read 、 写入 Write 、 FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);byte[] buffer = new byte[1024 * 1024 * 5]; // 5MBint r = fsRead.Read(buffer, 0, buffer.Length);// 将字节数组 编码格式解码string s = Encoding.Default.GetString(buffer, 0, r);// 关闭流fsRead.Close();// 释放资源fsRead.Dispose();// 文件流写入string path = @"F:xue_x";using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)){string str = "写入的内容";//byte[] buffer = new byte[1024 * 1024 * 5]; // 5MBbyte[] buffer = Encoding.Default.GetBytes(str);// 写入的内容、起点、终点、fsRead.Write(buffer, 0, buffer.Length);}Console.WriteLine("存在");
本文发布于:2024-01-28 10:00:30,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064072346621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |