最新【(11),字节大牛教你手撕大数据开发学习

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

♉ 自定义各种方法的行为的枚举
枚举描述
FileAccess指定对文件的读取和写入访问权限。
FileShare指定已在使用的文件所允许的访问级别。
FileMode指定是保留还是覆盖现有文件的内容,以及创建现有文件的请求是否会引发异常。
FileAccess 定义文件的读取、写入或读/写访问权限的常量
[System.Flags]
public enum FileAccess

Read1对文件的读访问。 可从文件中读取数据。 与 Write 组合以进行读写访问。
ReadWrite3对文件的读写访问权限。 可从文件读取数据和将数据写入文件。
Write2文件的写访问。 可将数据写入文件。 与 Read 组合以进行读写访问。
FileShare 包含用于控制其他FileStream对象对同一文件可以具有的访问类型的常数
[System.Flags]
public enum FileShare

Delete4允许随后删除文件。
Inheritable16使文件句柄可由子进程继承。 Win32 不直接支持此功能。
None0谢绝共享当前文件。 文件关闭前,打开该文件的任何请求(由此进程或另一进程发出的请求)都将失败。
Read1允许随后打开文件读取。 如果未指定此标志,则文件关闭前,任何打开该文件以进行读取的请求(由此进程或另一进程发出的请求)都将失败。 但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
ReadWrite3允许随后打开文件读取或写入。 如果未指定此标志,则文件关闭前,任何打开该文件以进行读取或写入的请求(由此进程或另一进程发出)都将失败。 但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
Write2允许随后打开文件写入。 如果未指定此标志,则文件关闭前,任何打开该文件以进行写入的请求(由此进程或另一进过程发出的请求)都将失败。 但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。
FileMode 指定操作系统打开文件的方式
public enum FileMode

Append6若存在文件,则打开该文件并查找到文件尾,或者创建一个新文件。 这需要 Append 权限。 FileMode.Append 只能与 FileAccess.Write 一起使用。 试图查找文件尾之前的位置时会引发 IOException 异常,并且任何试图读取的操作都会失败并引发 NotSupportedException 异常。
Create2指定操作系统应创建新文件。 如果此文件已存在,则会将其覆盖。 这需要 Write 权限。 FileMode.Create 等效于这样的请求:如果文件不存在,则使用 CreateNew;否则使用 Truncate。 如果该文件已存在但为隐藏文件,则将引发 UnauthorizedAccessException异常。
CreateNew1指定操作系统应创建新文件。 这需要 Write 权限。 如果文件已存在,则将引发 IOException异常。
Open3指定操作系统应打开现有文件。 打开文件的能力取决于 FileAccess 枚举所指定的值。 如果文件不存在,引发一个 FileNotFoundException 异常。
OpenOrCreate4指定操作系统应打开文件(如果文件存在);否则,应创建新文件。 如果用 FileAccess.Read 打开文件,则需要 Read权限。 如果文件访问为 FileAccess.Write,则需要 Write权限。 如果用 FileAccess.ReadWrite 打开文件,则同时需要 ReadWrite权限。
Truncate5指定操作系统应打开现有文件。 该文件被打开时,将被截断为零字节大小。 这需要 Write 权限。 尝试从使用 FileMode.Truncate 打开的文件中进行读取将导致 ArgumentException 异常。
♊ 常用方法
Copy(String, String, Boolean) 将现有文件复制到新文件。 允许覆盖同名的文件。
public static void Copy (string sourceFileName, string destFileName, bool overwrite);

参数

sourceFileName

string

要复制的文件。

destFileName

string

目标文件的名称。 它不能是一个目录或现有文件。

overwrite

bool

如果可以覆盖目标文件,则为 true;否则为 false

示例

以下示例将文件复制到 C:\archives\2008 备份文件夹。 它使用 方法的两个 Copy 重载,如下所示:

  • 它首先使用 File.Copy(String, String) 方法重载来复制文本 (.txt) 文件。 代码演示此重载不允许覆盖已复制的文件。
  • 然后,它 File.Copy(String, String, Boolean) 使用 方法重载将图片 (.jpg 文件) 。 该代码演示此重载允许覆盖已复制的文件。
string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files.
    foreach (string f in txtList)
    {

        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied.
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}

Create(String) 在指定路径中创建或覆盖文件
public static System.IO.FileStream Create (string path);

参数

path

string

要创建的文件的路径及名称。

返回

FileStream

一个 FileStream,它提供对 path 中指定的文件的读/写访问。

示例

以下示例在指定的路径中创建文件,将一些信息写入该文件,然后从该文件中读取。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            // Create the file, or overwrite if the file exists.
            using (FileStream fs = File.Create(path))
            {
                byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }

            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

Create(String, Int32, FileOptions) 创建或覆盖指定路径中的文件,指定缓冲区大小和一个描述如何创建或覆盖该文件的选项
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options);

参数

path

string

要创建的文件的路径及名称。

bufferSize

int

用于读取和写入到文件的已放入缓冲区的字节数。

options

FileOptions

FileOptions 值之一,它描述如何创建或覆盖该文件。

返回

FileStream

具有指定缓冲区大小的新文件。

示例

以下示例演示如何在创建文件流时使用异步值。

using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteToFile();
        }

        static async void WriteToFile()
        {
            byte[] bytesToWrite = Encoding.Unicode.GetBytes("example text to write");
            using (FileStream createdFile = File.Create("c:/Temp/testfile.txt", 4096, FileOptions.Asynchronous))
            {
                await createdFile.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
            }
        }
    }
}

Delete(String) 删除指定的文件
public static void Delete (string path);

参数

path

string

要删除的文件的名称。 不支持通配符。

示例

以下示例将文件组复制到 C:\archives\2008 备份文件夹,然后从源文件夹中将其删除。

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files.
    foreach (string f in txtList)
    {

        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied.
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}

Exists(String) 确定指定的文件是否存在
public static bool Exists (string? path);

参数

path

string

文件或目录的路径。

返回

bool

如果 path 指向现有目录,则为 true;如果该目录不存在或者在尝试确定指定目录是否存在时出错,则为 false

示例

下面的示例确定文件是否存在。

string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");

GetCreationTime(String) 返回指定文件或目录的创建日期和时间
public static DateTime GetCreationTime (string path);

参数

path

string

目录的路径。

返回

DateTime

一个设置为指定目录的创建日期和时间的结构。 该值用本地时间表示。

示例

下面的示例演示了 GetCreationTime

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Get the creation time of a well-known directory.
            DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);

            // Give feedback to the user.
            if (DateTime.Now.Subtract(dt).TotalDays > 364)
            {
                Console.WriteLine("This directory is over a year old.");
            }
            else if (DateTime.Now.Subtract(dt).TotalDays > 30)
            {
                Console.WriteLine("This directory is over a month old.");
            }
            else if (DateTime.Now.Subtract(dt).TotalDays <= 1)
            {
                Console.WriteLine("This directory is less than a day old.");
            }
            else
            {
                Console.WriteLine("This directory was created on {0}", dt);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Move(String, String) 将指定文件移到新位置,提供要指定新文件名的选项
public static void Move (string sourceFileName, string destFileName);

参数

sourceFileName

string

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

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

failed: {0}", e.ToString());
}
}
}


###### Move(String, String) 将指定文件移到新位置,提供要指定新文件名的选项



public static void Move (string sourceFileName, string destFileName);


**参数**



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


[外链图片转存中...(img-9GxzDHxm-1715813822182)]
[外链图片转存中...(img-Mhecb3l4-1715813822182)]
[外链图片转存中...(img-yOZPyVLj-1715813822182)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 12
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值