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 ...
随机推荐
- mysql之字符编码问题
mysql编码分为服务端编码和客户端编码两大类字段编码, 表编码, 数据库编码这些编码都属于服务端编码,服务端编码决定你可以存哪些字符以及这些字符要哪种规则排序.字段编码优先级最高. 你插入用什么码属 ...
- oracle net manager的配置文件tnsnames.ora位置
配置文件所在的路径:C:\app\Ling-PC\product\11.2.0\client_1\network\admin (红色为安装的盘符位置)
- IRunningObjectTable接口
IRunningObjectTable接口
- app.js ejs 转换为html
var express = require('express');var path = require('path');var favicon = require('serve-favicon');v ...
- linux C学习笔记01--makefile
不知不觉毕业五年了,以前学的linux基本都忘了,重新温习起来吧! 下面是自己写的makefile文件,供新手和自己回头时查阅 CC=gcc EXE=c.out CCC=g++ EEE=cc.out ...
- Spring —— 三种配置数据源的方式:spring内置、c3p0、dbcp
01.Spring内置数据源配置Class:DriverManagerDataSource全限定名:org.springframework.jdbc.datasource.DriverManagerD ...
- 客户端连接linux经常间隔性断开链接
起因 在使用SecureCRT通过telnet或SSH访问linux时,总是出现过段时间操作就会断开连接提示重连的问题.起初以为是网络不稳定造成的,但我测试发现在服务器端一直可以ping通客户端IP, ...
- Scrum会议10.20
Scrum会议 组名称:好好学习 项目名称:记账本 参会成员:林莉(Master)胡丽娜 汪东涵 宫丽君 时间:2016.10.20 已完成内容: 1.理解项目和代码. 2.讨论新功能. 计划完成 ...
- 常用UML模型简要小结
关系: 关联(组合,生命周期相同:聚合,物以类聚),依赖,泛化(继承),实现 还有 包含,细化复用已有用例:扩展,非必要主要的用例 图: 1.用例图:就是描述一个功能场景(集合),其实用例编写(前后置 ...
- 第二章 第二个spring-boot程序(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5336369.html 上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序.这一节会引入s ...