在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#异步将文本内容写入文件的更多相关文章

  1. java 将内容写入文件 txt

    @Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...

  2. JAVA-将内容写入文件并导出到压缩包

    取出数据库表中的内容写入到文件,并将所有文件写入到压缩包最终导出到指定的某目录下        //导出的压缩包格式  xxxx_date        Date currentTime = new ...

  3. C# 读取txt文本内容写入到excel

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. IOS应用发布NSLog的注释及使用重定向,把控制台内容写入文件

    1 选择工程的Target -> Build Settings -> Preprocessor Macros. 如图,默认 Debug项,是“DEBUG=1”. 2 在程序中设置全局宏定义 ...

  5. c#读取并异步写入文件,简单版,指定编码,保持原格式。

    1.同步读取和写入 StreamReader objReader = new StreamReader("E://workspace//zzz//read.txt", Encodi ...

  6. C#读取和写入文件

    一.读取文件 如果你要读取的文件内容不是很多, 可以使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding) ...

  7. 【php性能优化】关于写入文件操作的取舍方案

    对于使用php对文件进行写入操作有两种方案一种使用 file_put_contents() 和 fopen()/fwrite()/fclose() 两种方案至于应该怎么选,我觉得应该分情况选择,下面是 ...

  8. NIO学习:使用Channel、Buffer写入文件

    NIO的效率要高于标准IO,因为NIO将最耗时的IO操作(填充和提取缓冲区)转移会操作系统.NIO以块为单位传输数据,相比标准IO的以字节为单位效率要高很多. 通道和缓冲时NIO的核心对象,每个NIO ...

  9. PrintWriter 和 BufferedWriter 写入文件.

    Ref: should I use PrintWriter to wrap BufferedWriter? The main reason for using PrintWriter is the w ...

随机推荐

  1. jquery表格增加删除后改变序号

    有个小bug,懒得修了. 目的:增加一行的时候,td第一列排序. 删除一行的时候,td第一列排序 <!DOCTYPE HTML> <html> <head> < ...

  2. javascript-test1

    var AAA = function(name, age) { this.name = name ; this.age = age; } AAA.prototype.getName = functio ...

  3. Linux_06------Linux的磁盘管理

    进制编码 * 3.格式化交换分区 * 4.启用交换分区 * * 1.2. * fdisk /dev/sdb * p * t * 6(分区编号) * L(查看编码列表),找到SWAP交换分区标号 * 8 ...

  4. shell中单引号和双引号

    在shell中声明变量后直接使用: #!/bin/bash na=zhagnsan ag=11 echo '$na is $ag years old' 输出:$na is $ag years old ...

  5. 定位frame 中的对象

    在web 应用中经常会出现frame 嵌套的应用,假设页面上有A.B 两个frame,其中B 在A 内,那么定位B 中的内容则需要先到A,然后再到B.switch_to_frame 方法可以把当前定位 ...

  6. ios app打ipa包

    在app上传到appstore之前,我们通常要经过打包测试的步骤,导出到testFlight中.或者其他第三方的平台里面进行测试,这时候就需要打包成ipa包导出.好了,废话不多说,上图.步骤如下: 1 ...

  7. HttpURLConnection请求网络数据的Post请求

    //--------全局变量----------- //注册Url    private String urlPath="http://101.200.142.201:8080/VideoP ...

  8. 反射类属性生成DataTable

    public class People //类名 { private static string name; //字段 private string sex;//字段 public string Se ...

  9. com.apache.dc.query.Query所属包名apache-common-sid.jar

    com.apache.dc.query.Query所属包名apache-common-sid.jar 首先这个类是基于HQL的,好多方法里面要传String clzz, 刚开始我真不知道这个参数传什么 ...

  10. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

    public class Three_03 { public static void main(String[] args) { for(int i=100;i<1000;i++){ int a ...