C#读写文本文件小结

除了创建、复制、移动和删除外,对文本文件最常用的操作就是进行读写,C#提供了非常多的方法来对文本文件进行读写,今天我们做个小结:

一、写入文件

1.File类的静态方法WriteAllText(改写方式)

程序代码
File.WriteAllText(Server.MapPath("a.txt"), "http://www.mzwu.com/\r\nhttp://www.hao123.com/\r\nhttp://www.163.com/");

2.File类的静态方法WriteAllLines(改写方式)

程序代码
File.WriteAllLines(Server.MapPath("a.txt"), new string[] { "http://www.mzwu.com/", "http://www.hao123.com/", "http://www.163.com/" });

3.File类的静态方法AppendAllText(追加方式)

程序代码
File.AppendAllText(Server.MapPath("a.txt"), "http://www.mzwu.com/\r\nhttp://www.hao123.com/\r\n");
File.AppendAllText(Server.MapPath("a.txt"), "http://www.163.com/");

4.StreamWriter对象的Write方法和WriteLine方法(true追加,false改写)

程序代码
StreamWriter sw = new StreamWriter(Server.MapPath("a.txt"), false);
sw.Write("http://www.mzwu.com/\r\n");
sw.WriteLine("http://www.mzwu.com/");
sw.WriteLine("http://www.163.com/");
sw.Close();

说明:当文件不存在时,以上方法都将创建一个新文件!

二、读取文件

1.File类的静态方法ReadAllText

程序代码
File.ReadAllText(Server.MapPath("a.txt"))

2.File类的静态方法ReadAllLines

程序代码
string[] content = File.ReadAllLines(Server.MapPath("a.txt"));
for (int i = 0; i < content.Length; i++)
{
    Response.Write(content[i] + "<br/>");
}

3.StreamReader对象的ReadToEnd方法

程序代码
StreamReader sr = new StreamReader(Server.MapPath("a.txt"));
Response.Write(sr.ReadToEnd());
sr.Close();

4.StreamReader对象的ReadLine方法

程序代码
StreamReader sr = new StreamReader(Server.MapPath("a.txt"));
while (!sr.EndOfStream)
{
    Response.Write(sr.ReadLine() + "<br/>");
}
sr.Close();

原始文本如下:

C#读写文本文件小结除了创建、复制、移动和删除外,对文本文件最常用的操作就是进行读写,C#提供了非常多的方法来对文本文件进行读写,今天我们做个小结:一、写入文件 1.File类的静态方法WriteAllText(改写方式) 程序代码 File.WriteAllText(Server.MapPath("a.txt"), "http://www.mzwu.com/\r\nhttp://www.hao123.com/\r\nhttp://www.163.com/"); 2.File类的静态方法WriteAllLines(改写方式) 程序代码 File.WriteAllLines(Server.MapPath("a.txt"), new string[] { "http://www.mzwu.com/", "http://www.hao123.com/", "http://www.163.com/" }); 3.File类的静态方法AppendAllText(追加方式) 程序代码 File.AppendAllText(Server.MapPath("a.txt"), "http://www.mzwu.com/\r\nhttp://www.hao123.com/\r\n"); File.AppendAllText(Server.MapPath("a.txt"), "http://www.163.com/"); 4.StreamWriter对象的Write方法和WriteLine方法(true追加,false改写) 程序代码 StreamWriter sw = new StreamWriter(Server.MapPath("a.txt"), false); sw.Write("http://www.mzwu.com/\r\n"); sw.WriteLine("http://www.mzwu.com/"); sw.WriteLine("http://www.163.com/"); sw.Close(); 说明:当文件不存在时,以上方法都将创建一个新文件! 二、读取文件 1.File类的静态方法ReadAllText 程序代码 File.ReadAllText(Server.MapPath("a.txt")) 2.File类的静态方法ReadAllLines 程序代码 string[] content = File.ReadAllLines(Server.MapPath("a.txt")); for (int i = 0; i < content.Length; i++) { Response.Write(content[i] + "
"); } 3.StreamReader对象的ReadToEnd方法 程序代码 StreamReader sr = new StreamReader(Server.MapPath("a.txt")); Response.Write(sr.ReadToEnd()); sr.Close(); 4.StreamReader对象的ReadLine方法 程序代码 StreamReader sr = new StreamReader(Server.MapPath("a.txt")); while (!sr.EndOfStream) { Response.Write(sr.ReadLine() + "
"); } sr.Close();

转载于:https://www.cnblogs.com/vanjob/archive/2009/10/07/5605678.html

C#读写文本文件小结相关推荐

  1. python csv读写方法_python中csv文件的若干读写方法小结

    如下所示: //用普通文本文件方式打开和操作 with open("'file.csv'") as cf: lines=cf.readlines() ...... //用普通文本方 ...

  2. python积累--读写文本文件实例

    转载请注明出处:python积累–读写文本文件实例 读写文件是最常见的 IO 操作.我们经常从文件读取输入,将内容写到文件. 读文件 在 Python 中,读文件主要分为三个步骤: 打开文件 读取内容 ...

  3. QT学习:读写文本文件

    一.使用QFile类读写文本 使用QFile类读写文本文件代码如下: (1)源文件"main.cpp"的具体实现代码如下: #include <QCoreApplicatio ...

  4. Qfile与QTextStream读写文本文件

    Qfile读取文本文件: void readwrite::readfile() {QString path = QFileDialog::getOpenFileName(this, "ope ...

  5. QT读写文本文件编码设置

    QT读写文本文件编码设置 一.编码知识科普 Qt常见的两种编码是:UTF-8和GBK ★UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM. ...

  6. 读写文本文件-StreamReader和StreamWriter

    1.读写文本文件 我们对文本文件的读写一般使用StreamReader和StreamWriter,因为不同的文本有不同的编码格式,这个StreamReader会帮我们自动处理,所以我们不需要关心文本文 ...

  7. Java读写文本文件

    使用字节流读写文本文件 使用字节流类FileInputStream读文本文件 FileInputStream 称为文件输入流,它是字节输入流 InputStream 抽象类的一个子类. 作用是将文件中 ...

  8. C#使用StreamReader类和StreamWriter类读写文本文件

    StreamReader类和StreamWriter类可以实现读写文本文件,这两个类都在命名空间System.IO下. using System; using System.Collections.G ...

  9. 用C语言读写文本文件

    本节主要讨论如何使用C语言读写文本文件. 本文引用自作者编写的下述图书; 本文允许以个人学习.教学等目的引用.讲授或转载,但需要注明原作者"海洋饼干叔 叔":本文不允许以纸质及电子 ...

最新文章

  1. JDK9新特性实战:简化流关闭新姿势。
  2. 网站内容收录除了原创性和质量其他因素也少不了
  3. LinearLayout和RelativeLayoutnbs…
  4. C#修改系统环境变量,调用批处理bat
  5. fork创建多个子进程
  6. K8S安全军规101:对CNCF最佳实践的扩充
  7. 一个html页面上显示dopost,Java遇见HTML-servlet(一)
  8. SpringBoot 阶段总结
  9. linux cpu load命令,Linux性能检测常用的10个基本命令
  10. app商品详情原数据 API ——淘宝/天猫
  11. jQuery 进度条实现
  12. java实现并查集算法
  13. ignore在mysql中什么意思_ignore是什么意思
  14. 兴业数金C语言笔试,2021兴业数金校园招聘C语言开发工程师职位
  15. 解决Photoshop CS3 输入中文不能显示 输入文字不能显示 输入文字显示缓慢
  16. 【Unity游戏开发】动画系统(二)2D动画
  17. 利用网络爬虫爬取知乎回答者的信息及回答内容
  18. 在PC上安装Android SDK与Android模拟器 - 使用Android Studio 3.1.1
  19. npoi获取合并单元格_梦琪小生 C# 如何使用NPOI操作Excel以及读取合并单元格等
  20. ChatGPT的各项超能力从哪儿来?万字拆解追溯技术路线图来了!

热门文章

  1. Kubernetes源码阅读笔记——Controller Manager(之三)
  2. 【BZOJ3073】[Pa2011]Journeys 线段树+堆优化Dijkstra
  3. 关于范式的一些简单理解
  4. C# 数组 二维数组
  5. UIView 弹出动画
  6. 【暴走漫画起源考】Part2:姚明脸
  7. c# 调用SQL Server存储过程返回值(转)
  8. quadTree 论文Real-Time Generation of Continuous吃透了
  9. 记录自己的技术点点滴滴
  10. Android JetPack ViewModel 源码解析