1、同步读取和写入

            StreamReader objReader = new StreamReader("E://workspace//zzz//read.txt", Encoding.GetEncoding("utf-8"));
string sLine = "";
ArrayList LineList = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null && !sLine.Equals(""))
LineList.Add(sLine);
}
objReader.Close(); string str = "";
foreach (var item in LineList)
{
str += item + Environment.NewLine;
}
string filePath = "E://workspace//zzz//" + DateTime.Now.Millisecond + ".txt";
FileStream fs = new FileStream(filePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("utf-8"));
//开始写入
sw.Write(str);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();

2、异步写入

        /// <summary>
/// 异步将xml内容写入文件
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
public static void XmlToFile(string str, string fileName, string encoding)
{
string filePath = "E://workspace_VS//zzz//" + fileName + DateTime.Now + ".xml";
byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(str.ToString());
FileStream writer = new FileStream(filePath, FileMode.Create, FileAccess.Write);
writer.BeginWrite(bytes, , str.Length, new AsyncCallback(endWrite), writer);
}
/// <summary>
/// 结束异步写入
/// </summary>
/// <param name="asr"></param>
private static void endWrite(IAsyncResult asr)
{
using (Stream str = (Stream)asr.AsyncState)
{
str.EndWrite(asr);
}
}

c#读取并异步写入文件,简单版,指定编码,保持原格式。的更多相关文章

  1. 15.swoole学习笔记--异步写入文件

    <?php //异步写入文件 $content="hello world"; swoole_async_writefile('2.txt',$content,function ...

  2. IO文件的读取,以及写入文件内容

    package zxc; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fi ...

  3. sublime python3中读取和写入文件时如何解决编码问题

    # -*- coding: utf-8 -*- #分析用户身份审核信息 #python 3.5 #xiaodeng #http://apistore.baidu.com/apiworks/servic ...

  4. PHP fsockopen 异步写入文件

    b.php <?php $url = 'http://fsc.com/a.php'; $param = array( 'name'=>'fdipzone', 'gender'=>'m ...

  5. Java文件读写操作指定编码方式防乱码

    读文件:BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高效读取. 可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够大了. 通常,Re ...

  6. Kafka消费者 从Kafka中读取数据并写入文件

    Kafka消费者 从Kafka中读取数据 最近有需求要从kafak上消费读取实时数据,并将数据中的key输出到文件中,用于发布端的原始点进行比对,以此来确定是否传输过程中有遗漏数据. 不废话,直接上代 ...

  7. 爬虫 写入文件时遇到gbk编码错误

    #获取视频地址 # 每次请求一次,然后写文件,这样可以规避多次请求触发反爬虫 r = requests.get('https://www.pearvideo.com/video_1522192') h ...

  8. Java文件读写操作指定编码方式。。。。。

    读: File file=new File(this.filePath);BufferedReader br=new BufferedReader(new InputStreamReader(new ...

  9. c++读取文件操作和写入文件

    在C++中与读取文件和写入文件简单操作有关的类分别有ifstream(文件读入).ofstream(文件写出).fstream (文件读入和写出). 名称 作用 ifstream 文件读入 ofstr ...

随机推荐

  1. nginx打包成rpm

    [root@localhost ~ ]#yum -y install lrzsz pcre pcre-devel zlib zlib-devel vim nrt-tools psmisc gcc gc ...

  2. focus /focusin /focusout /blur 事件

    事件触发时间 focus:当focusable元素获得焦点时,不支持冒泡:focusin:和focus一样,只是此事件支持冒泡:blur:当focusable元素失去焦点时,不支持冒泡:focusou ...

  3. 用jQuery基础

    要使用jQuery要引用jQuery文件,在头标签中引用 1 <script src="jquery-1.11.2.min.js"></script>   ...

  4. dvaJs使用注意事项

    项目参考地址 dva-yicha 1. 使用路由跳转的方式 (1)所有的路由跳转功能都放到 dva/router 里面的 import { routerRedux } from 'dva/router ...

  5. bzoj1053题解

    [题意分析] 本题中,x被称为反质数,当且仅当没有任意一个严格小于x的正整数的约数个数大于x的约数个数.求不超过N的最大反质数. [解题思路] 数据范围中最大的N=2*109. 首先可以证明,不超过N ...

  6. NX二次开发-创建直线(起点-向量方向-长度)UF_CURVE_create_line

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_csys.h> #include < ...

  7. NX二次开发-UFUN旋转视图UF_VIEW_rotate_view

    NX11+VS2013 #include <uf.h> #include <uf_view.h> #include <uf_obj.h> #include < ...

  8. spring其他配置 (3)

    目录 一.自动装配 Autowired 二.bean的作用于singleton,prototype 三.引入外部资源properties文件 四.SpEL表达式 (可以为属性进行动态的赋值) 五.通过 ...

  9. C 函数指针详解

    一 通常的函数调用    一个通常的函数调用的例子://自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int a ...

  10. [POI2011]SMI-Garbage

    题目描述 http://main.edu.pl/en/archive/oi/18/smi The Byteotian Waste Management Company (BWMC) has drast ...