文件(File)

来源:互联网 发布:pslogo美工基础知识 编辑:程序博客网 时间:2024/05/28 16:25
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace File类{    class Program    {        static void Main(string[] args)        {            string path = @"D:\FileTest\1707A\aa.txt";            string destPath = @"D:\FileTest\1707A\aa.txt";            //Test01(path);            //Test01("王垚.txt");            //Test02(path, destPath);            //Test03(path);            Test04(path);        }        //文件创建        static void Test01(string path)        {            //文件的创建            if (File.Exists(path))            {                Console.WriteLine("文件已经存在");            }            else            {                //如果不存在则创建文件                File.Create(path);            }        }        //文件的移动  和文件的重命名        static void Test02(string sourcePath,string destPath)        {            File.Move(sourcePath, destPath);        }        static void Test03(string path)        {            File.Delete(path);        }        static void Test04(string path)        {            //从文件读取数据的流            FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read);            byte[] byteArr = new byte[4096];            int len = 0;            string destPath = @"D:\FileTest\1707A\Newaa.txt";            //写入文件的流            FileStream fsWrite = File.Open(destPath, FileMode.OpenOrCreate, FileAccess.Write);            //len表示 在调用Read方法的时候  从文件中实际读取的字节数            while ((len = fs.Read(byteArr, 0, byteArr.Length)) != 0)            {                fsWrite.Write(byteArr, 0, len);            }        }    }}
原创粉丝点击