C# path类:操作路径、File类:操作文件、文件流读写

2023-05-16

路径操作

            string str = @"F:\视频\C#全套视频教程\视频教程之10-面向对象多态\(第十二天)\video\1、复习.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_xi\1.txt
            //创建文件
            File.Create(@"F:\xue_xi\1.txt");

            // 删除文件
            File.Delete(@"F:\xue_xi\1.txt");

            string path = @"F:\xue_xi\1.txt";
            string path1 = @"F:\xue_xi\2.txt";

            //复制(原文件、新文件)
            File.Copy(path, path1);

            //文件读写(一)
            //读取
            string path = @"F:\xue_xi\1.txt";
            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_xi\1.txt";
            string str = "今天真不错";
            // 字符串 转 字节数组
            byte[] buffer = Encoding.Default.GetBytes(str);
            File.WriteAllBytes(path, buffer);
            Console.WriteLine("写入成功");

            //文件读写(二)

            //按行读取
            string path = @"F:\xue_xi\1.txt";
            string[] contents = File.ReadAllLines(path, Encoding.Default);
            foreach (string line in contents)
            {
                Console.WriteLine(line);
            }
            // 全读取 
            string path = @"F:\xue_xi\1.txt";
            string str = File.ReadAllText(path, Encoding.Default);
            Console.WriteLine(str);

            // 行方式 写入
            string path = @"F:\xue_xi\1.txt";
            File.WriteAllLines(path, new string[] { "对对对", "是是是", "你说的都对 " });
            Console.WriteLine("已经写入");

            // 全写入
            string path = @"F:\xue_xi\1.txt";
            File.WriteAllText(path, "对对对对对对对对对对对对对对对对对对对对对,是是是是是是是是是");
            Console.WriteLine("已经写入");
            
            // 追加
            string path = @"F:\xue_xi\1.txt";
            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];  // 5MB
            int 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];  // 5MB
                byte[] buffer = Encoding.Default.GetBytes(str);
                // 写入的内容、起点、终点、
                fsRead.Write(buffer, 0, buffer.Length);
            }


            Console.WriteLine("存在");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# path类:操作路径、File类:操作文件、文件流读写 的相关文章

随机推荐