C# File类API:

目录

File:

创建文件 Create()/CreateText():

打开文件Open()

判断指定的文件是否存在Exists()

复制文件Copy():

删除文件Delete():

移动文件Move():

替换文件Replace():

读取文件:ReadAllBytes():

ReadAllLines():

ReadAllText():

ReadLines():


File:

提供用于创建、复制、删除、移动和打开单个文件的静态方法,并有助于创建FileStream对象。

FIle是静态的,不需要创建它的实例就可以使用它的方法。

创建文件 Create()/CreateText():

Create()

CreateText()

用于创建一个新文件,如果文件已存在,则会覆盖

 private void button1_Click(object sender, EventArgs e)
 {
     // 路径
     string path = @"D:\FIle类API";
     //文件名
     string fileName = "fileName.txt";
     //文件的完整路径
     string fullPath = Path.Combine(path, fileName);

     // using子句,自动帮开发者关闭流
     using (StreamWriter sw = File.CreateText(fullPath))
     {
         // StreamWriter功能比FileStream单一些,但使用方便,可以直接写入字符串,而FileStream要求写入byte[]
         sw.WriteLine("hello");
         sw.WriteLine("world");
     }
 }

打开文件Open()

File.Open(string path, FileMode mode, FileAccess access)

打开指定路径上的FileStream,具有带读、写或读/写访问的指定模式和指定的共享选项。

参数:

path源文件名,要打开的指定文件。

mode模式:用于指定在文件不存在时是否创建该文件,并确定是要保留还是覆盖现有文件的内容。

access:指定对文件的读写和写入访问权限

private void button2_Click(object sender, EventArgs e)
{
    // 打开文件的路径
    string path = @"D:\abc\aaa\bbb\课堂笔记.txt";
    using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite))
    {
        byte[] bytes = new byte[fs.Length];
        int count = fs.Read(bytes, 0, bytes.Length);
        string result = Encoding.UTF8.GetString(bytes,0,bytes.Length);
        richTextBox1.Text = result;
    }
}

判断指定的文件是否存在Exists()

Exists(),返回Boolean,如果存在,返回true,不存在则返回false,

string filepath = @"D:\abc\课堂笔记.txt";
Console.WriteLine(File.Exists(filepath));

复制文件Copy():

(复制一个新的文件)

Copy(sourceFileName,destFileName) 将现有文件复制到新的文件。不允许覆盖同名文件。

Copy(sourceFileName,destFileName,overwrite) 将现有文件复制到新的文件,允许覆盖同名文件。

sourceFileName:要复制的文件,

destFileName:目标文件的名称。这不能是目录或现有文件。

overwrite:如果目标文件已存在,则true替换目标文件,否则,false

private void button4_Click(object sender, EventArgs e)
{
    string sourceFileName = @"C:\Users\asus\Desktop\文件1.txt";
    string newFileName = @"C:\Users\asus\Desktop\文件111本.txt";
    if (File.Exists(sourceFileName)&& !File.Exists(newFileName))
    {
        File.Copy(sourceFileName, newFileName);
    }
}

删除文件Delete():

string newFileName = @"C:\Users\asus\Desktop\文件111本.txt";
File.Delete(newFileName);

移动文件Move():

将指定文件移动到新位置,并提供指定新文件名的选项。

Move(string sourceFileName,string destFileName)

参数:

sourceFileName :要移动的文件的名称。可以包括相对或绝对路径。

destFileName:文件的新路径和名称。

private void button6_Click(object sender, EventArgs e)
{
    string sourceFileName = @"C:\Users\asus\Desktop\文件123本.txt";
    string newFileName = @"C:\Users\asus\Desktop\文件456本.txt";
    File.Move(sourceFileName, newFileName);
}

替换文件Replace():

Replace(string sourceFileName,string destinationFileName,string destinationBackupFileName)

参数:

sourceFileName:要替换的源文件的名称。

destinationFileName:要被替换的目标文件的名称。

destinationBackupFileName:备份文件名称

 private void button7_Click(object sender, EventArgs e)
 {
        //要替换的源文件的名称。
     string sourceFileName = @"C:\Users\asus\Desktop\文件456本.txt";
        //要被替换的目标文件的名称。
     string destinationFileName = @"C:\Users\asus\Desktop\文件22222本.txt";
        //备份文件的名称。
     string BackupFileName = @"C:\Users\asus\Desktop\原文件备份.txt";
     File.Replace(sourceFileName, destinationFileName, BackupFileName);
 }

读取文件:ReadAllBytes():

Byte[]包含文件内容的字节数组。

ReadAllBytes是一个简单且直接的方法,适用于快速读取整个文件内容的场景,如果在处理大文件时,考虑使用更高效的读取方式。

把流中的内容读取byte[]中。Read()在读取大量内容,会阻塞。大量内容读取,建议使用ReadAsync()

ReadAllLines():

打开文本文件,读取文件的所有行,然后关闭该文件。

ReadAllText():

打开文本文件,读取文件中的所有文本,然后关闭该文件。

ReadLines():

读取文件的行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值