C#异步将文本内容写入文件
在C#/.NET中,将文本内容写入文件最简单的方法是调用 File.WriteAllText() 方法,但这个方法没有异步的实现,要想用异步,只能改用有些复杂的 FileStream.WriteAsync() 方法。
使用 FileStream.WriteAsync() 有2个需要注意的地方,1是要设置bufferSize,2是要将useAsync这个构造函数参数设置为true,示例调用代码如下:
public async Task CommitAsync()
{
var bits = Encoding.UTF8.GetBytes("{\"text\": \"test\"}");
using (var fs = new FileStream(
path: @"C:\temp\test.json",
mode: FileMode.Create,
access: FileAccess.Write,
share: FileShare.None,
bufferSize: ,
useAsync: true))
{
await fs.WriteAsync(bits, , bits.Length);
}
}
看这个方法的帮助文档中对useAsync参数的说明:
// useAsync:
// Specifies whether to use asynchronous I/O or synchronous I/O. However, note
// that the underlying operating system might not support asynchronous I/O,
// so when specifying true, the handle might be opened synchronously depending
// on the platform. When opened asynchronously, the System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)
// and System.IO.FileStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)
// methods perform better on large reads or writes, but they might be much slower
// for small reads or writes. If the application is designed to take advantage
// of asynchronous I/O, set the useAsync parameter to true. Using asynchronous
// I/O correctly can speed up applications by as much as a factor of 10, but
// using it without redesigning the application for asynchronous I/O can decrease
// performance by as much as a factor of 10.
从中可以得知,只有设置useAsync为true,才真正使用上了异步IO。
C#异步将文本内容写入文件的更多相关文章
- java 将内容写入文件 txt
@Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...
- JAVA-将内容写入文件并导出到压缩包
取出数据库表中的内容写入到文件,并将所有文件写入到压缩包最终导出到指定的某目录下 //导出的压缩包格式 xxxx_date Date currentTime = new ...
- C# 读取txt文本内容写入到excel
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- IOS应用发布NSLog的注释及使用重定向,把控制台内容写入文件
1 选择工程的Target -> Build Settings -> Preprocessor Macros. 如图,默认 Debug项,是“DEBUG=1”. 2 在程序中设置全局宏定义 ...
- c#读取并异步写入文件,简单版,指定编码,保持原格式。
1.同步读取和写入 StreamReader objReader = new StreamReader("E://workspace//zzz//read.txt", Encodi ...
- C#读取和写入文件
一.读取文件 如果你要读取的文件内容不是很多, 可以使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding) ...
- 【php性能优化】关于写入文件操作的取舍方案
对于使用php对文件进行写入操作有两种方案一种使用 file_put_contents() 和 fopen()/fwrite()/fclose() 两种方案至于应该怎么选,我觉得应该分情况选择,下面是 ...
- NIO学习:使用Channel、Buffer写入文件
NIO的效率要高于标准IO,因为NIO将最耗时的IO操作(填充和提取缓冲区)转移会操作系统.NIO以块为单位传输数据,相比标准IO的以字节为单位效率要高很多. 通道和缓冲时NIO的核心对象,每个NIO ...
- PrintWriter 和 BufferedWriter 写入文件.
Ref: should I use PrintWriter to wrap BufferedWriter? The main reason for using PrintWriter is the w ...
随机推荐
- jQuery鼠标事件汇总
鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的. 1.click事件:点击鼠标左键时触发 $('p').click(function(){}); 示例: $('p').click(func ...
- (转)django上传文件
本文转自:http://www.cnblogs.com/linjiqin/p/3731751.html 另: 本文对原文做了适当修改 更为详细的介绍可以参考官方文档. emplate html(模板 ...
- WPF的图片操作效果(一):RenderTransform
一.RenderTransform类的成员: 1.TranslateTransform 平移效果 2.RotateTransform 旋转效果 3.ScaleTransform 缩放效果 ...
- win7,win8.1下hosts文件无法修改的快速解决办法
一,找到C:\Windows\System32\drivers\etc,下hosts文件复制一份到桌面: 二,使用notepad++或其他编辑器修改桌面复制出来的那份HOSTS: 三,将修改后的文件复 ...
- python面向对象个人总结
基础概念:面向对象其实就是类与对象的使用. 类是模板,对象是实例.模板创建实例,实例去类里面去执行类的方法.类的例子: class Foo: def Bar(self): ...
- debian vi
这次用DigitalOcean VPS发现vi的方向键变成字母,没办法正常使用,搜索了下找到了解决办法. 1 vi /etc/vim/vimrc.tiny 找到set compatible改为set ...
- 项目使用中Linq使用总结
项目使用中Linq使用总结 本文旨在和网友分享Linq在项目中的实践,曾经我参与过的项目都能看见Linq的影子.(LinqTosql.LinqToString.LinqToXML.LinqToEnti ...
- haploview出现“more than two alleles”的解决方法
弹出“more than two alleles”的错误是因为ped文件中一个SNP位点上存在两个以上的等位基因,haploview连锁分析时默认为只有两个等位基因,因此我们要去掉超过两位等位基因的S ...
- Light OJ 1019 - Brush (V)(图论-dijkstra)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1019 题目大意:Tanvir想从节点1的位置走到节点n的位置, 输出最短距离, ...
- Correspondence / ˏkɔris'pɔndәns / dictionary10-800.doc
I have taken courses in office administration, typing,reports and correspondence writing. Correspond ...