c# 文件操作类 path file filestream等

一、Path路径类的常用操作

 string a = "Z:\\音乐\\1.mp3";
            uiButton1.Text = Path.GetDirectoryName(a);  // z:\音乐
            uiButton1.Text = Path.GetFileNameWithoutExtension(a); // 1
            uiButton1.Text = Path.GetExtension(a); // .mp3
            uiButton1.Text = Path.GetFileName(a);  //1.mp3
            uiButton1.Text = Path.ChangeExtension(a, ".wav"); //z:\音乐\1.wav

  

改变扩展名:Path.ChangeExtension()

五、目录操作directory

          string Path = @"c:\新建文件夹";
            //新建目录
            Directory.CreateDirectory(Path);
            //删除目录,true可以删有内容的文件夹
            Directory.Delete(Path, true);
            //目录的移动
            Directory.Move(Path, newpath);
            //获取目录内容,为全路径"c:/1.txt"
            string[] filenames=Directory.GetFiles(Path);
            string[] filenames2 = Directory.GetFiles(Path,"*.mp3");//匹配模式
           //获取子目录,包括路径
           string[] direcs= Directory.GetDirectories(@"d:\1");
          //指定目录是否存在
          bool a = Directory.Exists(@"d:\1");

 

 

二、文件操作

           //创建一个新文件
            File.Create(@"c/1.txt");
            //复制
            File.Copy(@"c/1.txt",@"d/new.doc");
            //删除
            File.Delete(@"c/1.txt")
            //移动/重命名,Move用法,给定一个旧文件,将其移到新位置。当然,原路径和新路径是可以一样的,原理还是使用了移动的原理而已。         
            File.move()

三、小文件的读取

file适合读取小文件,是一次性全部加载

读取三种方式:

File.ReadAllBytes(); //可读取所有类型文件,获得byte二进制数组,需要编对应码才能获得原始内容
File.ReadAllLines();  //文本,按行
File.ReadAllText();  //文本

 static void Main(string[] args)
        {
            //ReadAllBytes以二进制byte方式读取文件内容
            string path = @"D:\Users\yaoyue\Desktop\1.txt";
            byte[] buffer= File.ReadAllBytes(path);
            //将二进制按照编码格式成字符串 utf8,gbk,gb2312等
            string str = Encoding.Default.GetString(buffer, 0, buffer.Length);
            string str2 = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

            //ReadAllLines读取文件内容
            string[] lines=File.ReadAllLines(path, Encoding.Default);//注意编码格式
            for (int i = 0; i < lines.Length; i++)
            {
                Console.WriteLine(lines[i]);
            }

            //ReadAllText
            string content = File.ReadAllText(path, Encoding.Default);
            Console.WriteLine(content);
        }

四、小文件的写入

三种方式:

File.WriteAllBytes   写所有文件
File.WriteAllLines   写文本,按行
File.WriteAllText    写文本
            //WriteAllBytes方式写入
            string str = "我是王五";
            //将字符串解码为字节数组
            byte[] buffer = Encoding.Default.GetBytes(str);
            File.WriteAllBytes(@"c:\1.txt", buffer);

            //WriteAllLines方式写入
            File.WriteAllLines(@"c:\1.txt", new string[] { "张三", "李思" });//每行只一个元素

            //WriteAllText方式写入
            File.WriteAllText(@"c:\1.txt", str);     

追加写法:

//上面的都是覆盖原文件,追加用append~
            File.AppendAllText(@"c:\1.txt", str);
            File.AppendAllLines(@"c:\1.txt", new string[] { "zddd","dddd" });
            File.AppendText();

六、文件流读取

方式  streamwriter  写字符,即文本格式

         streamreader  读文本格式

         filestream   所有类型都可以byte

 filestream 读:

  可以用using自动关闭和释放资源, using(){..}

     //FileStream 读取字节的,所有文件可以读
            string path = @"D:\1.txt";
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                //声明字节数组,存放文件流数据,大小自定
                byte[] buffer = new byte[1024 * 1024 * 5];
                //将fs指定文件字节放入字节数组buffer,返回实际使用字节数,如果单纯放入,可以不接受返回值
                int realcount = fs.Read(buffer, 0, buffer.Length);
                //编码得到字符串,0到realcount位置为文件内容,其他为null填充,所以编码必须有范围
                string str = Encoding.Default.GetString(buffer, 0, realcount);

                Console.Write(str);
            }

 filestream 写:

 using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                string str = "看我覆盖不覆盖";
                //写的时候必须是byte形式,所以必须转为byte[]
                byte[] buffer=Encoding.Default.GetBytes(str);
                //写入
                fs.Write(buffer,0,buffer.Length);

            }

 

streamwriter,直接写文本,不用转byte

        string str = "写入的内容";     
            string path = @"D:\1.txt";
            using (StreamWriter sr = new StreamWriter(path,true, Encoding.Default))//路径,追加否,编码方式
            {
                sr.Write(str);
               
            }

 streamreader,读文本,不用转byte

 static void Main(string[] args)
        {
            string path = @"D:\1.txt";
            using (StreamReader sr = new StreamReader(path, Encoding.Default)) //路径,编码方式
            {
                while (!sr.EndOfStream) //endofstream 如果当前流位置位于流的末尾,则为 true;否则为 false。
                {
                    string contentLine = sr.ReadLine(); //读取一行 
                    Console.WriteLine(contentLine);
                }
                Console.ReadKey();
            }
        }

  

posted @ 2021-03-10 21:20  遥月  阅读(281)  评论(0)    收藏  举报