当前位置: 首页 > wzjs >正文

怎么给网站做网站地图计算机培训班培训费用

怎么给网站做网站地图,计算机培训班培训费用,政府类型网站建设方案,苹果手机如何做微电影网站文件读取是指使用 C# 程序从计算机文件系统中获取文件内容的过程。将存储在磁盘上的文件内容加载到内存中,供程序处理。主要类型有:文本文件读取(如 .txt, .csv, .json, .xml);二进制文件读取(如 .jpg, .pn…

        文件读取是指使用 C# 程序从计算机文件系统中获取文件内容的过程。将存储在磁盘上的文件内容加载到内存中,供程序处理。主要类型有:文本文件读取(如 .txt, .csv, .json, .xml);二进制文件读取(如 .jpg, .png, .exe)。

同步方法

File 

ReadAllText

是 C# 中一个简单易用的文件写入方法,它会创建新文件,如果文件已存在则覆盖。

string path = @"D:\CSharpProject\File\MyTest.txt";
string str = File.ReadAllText(path);

 ReadLines

获取文件中每行数据组成的集合

string path = @"D:\CSharpProject\File\MyTest.txt";
var list = File.ReadLines(path).ToList();
Console.WriteLine(string.Join(",", list));

ReadAllLines

会一次性将整个文本文件的所有行读取到一个字符串数组中。

string path = @"D:\CSharpProject\File\MyTest.txt";
string[] list = File.ReadAllLines(path);
Console.WriteLine(string.Join(",", list));

ReadAllBytes

一个用于读取二进制文件的方法,它会将整个文件内容读取到一个字节数组中。

string path = @"D:\CSharpProject\File\MyTest.txt";
byte[] str = File.ReadAllBytes(path);

StreamReader

StreamReader 是 System.IO 命名空间中的一个类,专门用于从流(如文件流)中读取字符数据。它提供了高效的文本读取能力,特别适合处理大型文本文件。

方法描述
Read()读取下一个字符
ReadLine()读取一行字符
ReadToEnd()读取从当前位置到流末尾的所有内容

简单使用

string path = @"D:\CSharpProject\File\MyTest.txt";
using (StreamReader reader = new StreamReader(path))
{string content = reader.ReadToEnd();Console.WriteLine(content);
}

逐行读取

using (StreamReader reader = new StreamReader("data.txt"))
{string line;while ((line = reader.ReadLine()) != null){Console.WriteLine(line);}
}

读取特定数量字符

char[] buffer = new char[100];
using (StreamReader reader = new StreamReader("file.txt"))
{int charsRead = reader.Read(buffer, 0, buffer.Length);string result = new string(buffer, 0, charsRead);
}

FileStream 

         FileStream 是 System.IO 命名空间中的一个类,提供了对文件进行低级读写操作的能力。与更高级的 StreamReader/StreamWriter 相比,FileStream 提供了更底层的控制,适合处理二进制文件或需要精细控制文件操作的场景。

using System.IO;string filePath = "example.bin";// 使用 using 语句确保资源被正确释放
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{byte[] buffer = new byte[fileStream.Length];int bytesRead = fileStream.Read(buffer, 0, buffer.Length);Console.WriteLine($"读取了 {bytesRead} 字节");// 处理 buffer 中的数据...
}

异步方法

File 

ReadAllTextAsync

是ReadAllText的异步实现,功能相同,不会阻塞主线程。

using System.IO;
using System.Threading.Tasks;public async Task<string> ReadFileContentAsync(string filePath)
{return await File.ReadAllTextAsync(filePath);
}

ReadAllLinesAsync

是ReadAllLines的异步实现,功能相同,不会阻塞主线程。

using System.IO;
using System.Threading.Tasks;public async Task<string[]> ReadFileAsync(string filePath)
{return await File.ReadAllLinesAsync(filePath);
}

ReadAllBytesAsync

是ReadAllBytes的异步实现,功能相同,不会阻塞主线程

using System.IO;
using System.Threading.Tasks;public async Task<byte[]> ReadBinaryFileAsync(string filePath)
{return await File.ReadAllBytesAsync(filePath);
}

StreamReader

 简单使用

using System.IO;
using System.Threading.Tasks;async Task<string> ReadFileAllTextAsync(string filePath)
{using (StreamReader reader = new StreamReader(filePath)){return await reader.ReadToEndAsync();}
}

异步逐行读取文件

using (StreamReader reader = new StreamReader("data.txt"))
{string line;while ((line = reader.ReadLine()) != null){Console.WriteLine(line);}
}

异步读取指定数量字符

async Task<string> ReadChunkAsync(string path, int chunkSize)
{using (var reader = new StreamReader(path)){char[] buffer = new char[chunkSize];int bytesRead = await reader.ReadBlockAsync(buffer, 0, chunkSize);return new string(buffer, 0, bytesRead);}
}

FileStream 

using System.IO;
using System.Threading.Tasks;async Task<byte[]> ReadFileFullyAsync(string filePath)
{using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, FileOptions.Asynchronous)) // 关键选项{byte[] buffer = new byte[fs.Length];await fs.ReadAsync(buffer, 0, buffer.Length);return buffer;}
}

对比

方法特点适用场景内存效率异步支持典型文件大小
File.ReadAllText一次性读取全部文本小文本文件处理<10MB
File.ReadAllTextAsync异步读取全部文本UI应用读取小文本<10MB
File.ReadAllLines按行读取为数组需要行处理的小文件<10MB
File.ReadAllLinesAsync异步按行读取UI应用需要行处理<10MB
File.ReadAllBytes读取二进制数据图片/音频等二进制文件<50MB
File.ReadAllBytesAsync异步读取二进制UI应用处理二进制<50MB
File.ReadLines延迟行迭代大文本文件处理10MB+
StreamReader灵活文本读取需要控制读取过程有(Async方法)任意大小
FileStream底层二进制读取大文件/随机访问任意大小

选择参考

参考项

推荐方法

场景举例

小文本文件处理 ( < 1MB )

ReadAllText 

ReadAllTextAsync

ReadAllLines 

ReadAllLinesAsync

配置文件读取

小型JSON/XML文件解析

模板文件加载

大文本文件处理 ( > 10MB )

File.ReadLines (LINQ兼容)

StreamReader (逐行处理)

StreamReader.ReadLineAsync (异步逐行)

日志文件分析

大型CSV数据处理

文本文件实时监控

二进制文件处理

File.ReadAllBytes / ReadAllBytesAsync (小文件)

FileStream (大文件)

图片/音视频处理

文件加密/解密

自定义二进制格式解析

同步和异步选择

  1. 使用同步方法

    • 简单的控制台应用程序

    • 需要简单快速完成的小文件操作

    • 在后台线程中执行的文件操作

  2. 使用异步方法

    • UI应用程序(避免界面冻结)

    • Web应用程序和服务(提高并发处理能力)

    • 大文件操作(避免阻塞主线程)

    • 需要响应性的应用程序

http://www.dtcms.com/wzjs/185840.html

相关文章:

  • 绍兴网站优化国内疫情最新情况
  • 广州网站建设亅新科送推广it培训机构学费一般多少
  • 网站建设云浪科技引流app推广软件
  • 科技类公司网站怎么设计全网营销国际系统
  • dw旅游网站怎么做网站提交
  • 哈尔滨微网站建设sem推广外包
  • 凡科互联网国内好的seo网站
  • 网站推广什么意思seo外链
  • 上海公共招聘网官方网站国家税务总局网
  • 淘宝网站怎么做的好坏宁波seo网站排名
  • 廊坊建网站泸州网站seo
  • 网页设计找什么工作seo优化方案报价
  • 大新网站制作全网引擎搜索
  • 公司画册设计网站搜狐三季度营收多少
  • 网站模板红黑全网搜索引擎
  • 网站开发需不需要考研seo优化工作怎么样
  • 怎么用手机做钓鱼软件或者网站安徽网站推广
  • 做盗版电影网站赚钱吗产品推广活动策划方案
  • 制作网站得多少钱百度广告优化
  • wordpress清理网站优化推广软件
  • 网站分类查询东莞关键词自动排名
  • 网站解析后显示建设中腾讯企业qq
  • 建设网站详细流程找文网客服联系方式
  • 深圳网站建设 东莞网站建设百度竞价托管外包
  • 做影视网站怎么挣钱seo技术培训广东
  • 为了选择合适的网络设计方案双滦区seo整站排名
  • 湛江市网站建设网络营销案例2022
  • 搭建网站多少时间青岛新闻最新今日头条
  • php做网站怎么样seo 优化顾问
  • wordpress如何自己写页面seo关键词使用