C# 中的 读取和写入文本文件函数

首页 / C#入门教程 / C# 中的 读取和写入文本文件函数

StreamReaderStreamWriter类用于从文本文件读取数据和向文本文件写入数据。

StreamReader类

StreamReader类还从抽象基类TextReader继承,TextReader表示用于读取一系列字符的readerx。下表说明了StreamReader类的一些方法

Sr.No. Method & 描述
1

public override void close()

它关闭流,并释放系统资源。

2

public override int Peek()

返回下一个可用字符,但不使用它。

3

public override int read()

从输入流中读取下一个字符,并将字符位置向前推进一。

以下示例演示了如何读取名为jamica.txt的文本文件。文件读取-

Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop
using System;
using System.IO;

namespace FileApplication {
   class Program {
      static void Main(string[] args) {
         try {
            //Create an instance of StreamReader to read from a file.
            //The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("c:/jamaica.txt")) {
               string line;

               //Read and display lines from the file until 
               //the end of the file is reached. 
               while ((line = sr.ReadLine()) != null) {
                  Console.WriteLine(line);
               }
            }
         } catch (Exception e) {
            //Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
         }
         Console.ReadKey();
      }
   }
}

StreamWriter类

StreamWriter类继承自表示writer的抽象类TextWriter,后者可以写入一系列字符。

Sr.No. Method & 描述
1

public override void close()

关闭当前StreamWriter对象。

2

public override void flush()

无涯教程网

清除当前writer的所有缓冲区,并导致将任何缓冲的数据写入流。

3

public virtual void Write(bool value)

将布尔值的文本表示形式写入文本字符串或流。

4

public override void write(Char Value)

将字符写入流。

5

public virtual void Write(decimal value)

将十进制值的文本表示形式写入文本字符串或

6

public virtual void Write(double value)

将8字节浮点值的文本表示形式写入文本字符串或流。

7

public virtual void Write(int value)

将4字节有符号整数的文本表示形式写入文本字符串或流。

8

public override void write(String Value)

将字符串写入流。

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-text-files.html

来源:LearnFk无涯教程网

9

public virtual void WriteLine()

将行结束符写入文本字符串或流。

下面的示例演示如何使用StreamWriter类-将文本数据写入文件

using System;
using System.IO;

namespace FileApplication {
   class Program {
      static void Main(string[] args) {
         string[] names = new string[] {"Zara Ali", "Nuha Ali"};
         
         using (StreamWriter sw = new StreamWriter("names.txt")) {

            foreach (string s in names) {
               sw.WriteLine(s);
            }
         }
         
         //Read and show each line from the file.
         string line = "";
         using (StreamReader sr = new StreamReader("names.txt")) {
            while ((line = sr.ReadLine()) != null) {
               Console.WriteLine(line);
            }
         }
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

Zara Ali
Nuha Ali

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

机器学习40讲 -〔王天一〕

算法面试通关40讲 -〔覃超〕

零基础学Java -〔臧萌〕

TypeScript开发实战 -〔梁宵〕

ZooKeeper实战与源码剖析 -〔么敬国〕

Spark性能调优实战 -〔吴磊〕

如何落地业务建模 -〔徐昊〕

中间件核心技术与实战 -〔丁威〕

好记忆不如烂笔头。留下您的足迹吧 :)