分享

C#读取和写入文本文件

 3dC 2017-03-22

Windows10用户联盟QQ群: 227270512


StreamReader和StreamWriter类用于读取和写入数据到文本文件。这些类从抽象基类流,支持读取和写入字节到一个文件流继承。

StreamReader类

StreamReader类也继承自抽象基类的TextReader表示读者读取一系列字符。下表描述了一些StreamReader类的常用方法:

S.N 方法名称及用途
1 public override void Close()
它关闭StreamReader对象和基础流,并释放与读者相关的所有系统资源
2 public override int Peek()
返回下一个可用字符,但并不使用它
3 public override int Read()
读取从输入流中的下一个字符,并通过一个字符前进的字符位置

例子:

下面的例子演示了名为读取Jamaica.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类继承自抽象类TextWriter代表writer,可以写入一系列字符。

下表显示了一些此类的最常用的方法:

S.N 方法名称及用途
1 public override void Close()
关闭当前StreamWriter对象和基础流
2 public override void Flush()
清除所有缓冲区当前的writer ,并导致所有缓冲的数据写入底层流
3 public virtual void Write(bool value)
写一个布尔值的文本字符串或流的文本表示。 (继承自TextWriter)
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 ) 
将一个字符串写入流
9 public virtual void WriteLine()
写入一个行结束的文本字符串或流

对于方法的完整列表,请访问微软的C#文档。

下面的例子演示了使用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


标签:C#    读取    写入    文本文件    
本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:易百教程 [http:/www.]
本文标题:C#读取和写入文本文件
本文地址:http://www./csharp/csharp_text_files.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    来自: 3dC > 《c#》

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多