using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 文件 { class FileTest { //using释放非托管资源 #region Path类的使用 /// <summary> /// Path类 /// </summary> public static void MPath() { string str = @"D:\工作\文件测试\test001.txt"; //获取文件名 Console.WriteLine(Path.GetFileName(str)); //获取文件名但是不包含文件扩展名 Console.WriteLine(Path.GetFileNameWithoutExtension(str)); //获取文件的扩展名 Console.WriteLine(Path.GetExtension(str)); //获取文件所在的全路径 Console.WriteLine(Path.GetFullPath(str)); //连接两个字符串作为路径 Console.WriteLine(Path.Combine(@"D:\工作\文件测试", "test001.txt")); } #endregion #region File类的使用(创建、删除、复制、移动) /// <summary> /// File类 /// </summary> public static void MFile() { File.Create(@"D:\工作\文件测试\test001.txt"); Console.WriteLine("创建成功!"); File.Delete(@"D:\工作\文件测试\test001.txt"); Console.WriteLine("删除成功!"); File.Copy(@"D:\工作\文件测试\test001.txt", @"D:\工作\文件测试\test002.txt"); Console.WriteLine("复制成功!"); File.Move(@"D:\工作\文件测试\test001.txt", @"D:\工作\文件测试\test003.txt"); Console.WriteLine("移动成功"); } #endregion #region File类的使用2 public static void MFileData() { byte[] buffer = File.ReadAllBytes(@"D:\工作\文件测试\test001.txt"); #region 支持的编码格式 //EncodingInfo[] en = Encoding.GetEncodings(); //foreach (var item in en) //{ // Console.WriteLine(item.DisplayName); //} #endregion #region 将字节数组转换为字符串 string s = Encoding.UTF8.GetString(buffer); Console.WriteLine(s); Console.WriteLine(buffer.ToString()); //有误 #endregion #region 将字节数组写入文件中 string str = "ClearLove,001,明凯,777"; //将字符串转换成字节数组 byte[] bufferw = Encoding.UTF8.GetBytes(str); //以字节的形式向计算机中写入文本文件 File.WriteAllBytes(@"D:\工作\文件测试\test002.txt", bufferw); Console.WriteLine("写入成功"); #endregion #region 从文件中读取数据到字节数组中 byte[] bufferr = File.ReadAllBytes(@"D:\工作\文件测试\test002.txt"); File.WriteAllBytes(@"D:\工作\文件测试\test003.txt", bufferr); Console.WriteLine("复制成功!"); #endregion } #endregion #region FileStream类的使用 /// <summary> /// FileStream类 /// </summary> public static void MFileStream() { #region FileStream读取文件 FileStream fsRead = new FileStream(@"D:\工作\文件测试\test002.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] buffer = new byte[1024]; //返回本次实际读取的有效字节数 int r = fsRead.Read(buffer, 0, buffer.Length); //将字节数组中每一个元素按照指定的编码格式解码成字符串 string s = Encoding.UTF8.GetString(buffer, 0, r); //关闭文件流 fsRead.Close(); //释放流所占用的资源 fsRead.Dispose(); Console.WriteLine(s); #endregion #region FileStream写入文件 //使用using using (FileStream fsWrite = new FileStream(@"D:\工作\文件测试\EDG.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { byte[] bufferw = Encoding.UTF8.GetBytes(s); fsWrite.Write(buffer, 0, bufferw.Length); } #endregion } #endregion #region FileStream类实现多媒体文件的复制 public static void MFileStreamData() { string source = @"D:\工作\文件测试\打上花火.mp3"; string target = @"D:\工作\测试\打上花火.mp3"; CopyFile(source, target); Console.WriteLine("复制成功!"); } public static void CopyFile(string source, string target) { //创建一个文件输入流 using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read)) { //创建一个文件输出流 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = new byte[12 * 1024 * 1024]; while (true) { int r = fsRead.Read(buffer, 0, buffer.Length); if (r == 0) { break; } fsWrite.Write(buffer, 0, r); } } } Console.WriteLine("复制成功!"); } #endregion #region FileStream类读取一个文本文件 public static void MstreamRW() { using (StreamReader sr = new StreamReader(@"D:\工作\文件测试\test003.txt", Encoding.UTF8)) { using (StreamWriter sw = new StreamWriter(@"D:\工作\文件测试\test002.txt", true)) //true表示追加 { while (!sr.EndOfStream) { sw.Write(sr.ReadLine()); } Console.WriteLine("复制成功!"); } } } #endregion static void Main(string[] args) { MPath(); MFile(); MFileData(); MFileStream(); MFileStreamData(); MstreamRW(); Console.ReadKey(); } } }
标签:文件,Console,C#,FileStream,WriteLine,using,操作,txt From: https://www.cnblogs.com/kaede/p/16831805.html