Latest web development tutorials

C # write text files

Input and Output C # file Input and Output C # file

StreamReader and StreamWriterclasses for data read and write text files. These classes inherit from the abstract base class Stream, Stream supports byte stream read and write files.

StreamReader class

StreamReader class inherits from the abstract base class TextReader, showing the reader to read a series of characters.

The following table lists some of theStreamReader class commonly used methods:

序号方法 & 描述
1public override void Close()
关闭 StreamReader 对象和基础流,并释放任何与读者相关的系统资源。
2public override int Peek()
返回下一个可用的字符,但不使用它。
3public override int Read()
从输入流中读取下一个字符,并把字符位置往前移一个字符。

For a complete list, please visit the Microsoft C # documents.

Examples

The following example demonstrates reading a file named Jamaica.txt. The following documents:

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 a StreamReader to read the file // using statement also close StreamReader
                using (StreamReader sr = new StreamReader ( "c: /jamaica.txt"))
                {
                    string line;
                   
                    // Read from the file and display the line until the end of the file while ((line = sr.ReadLine ())! = Null)
                    {
                        Console.WriteLine (line);
                    }
                }
            }
            catch (Exception e)
            {
                // Display an error message to the user Console.WriteLine ( "The file could not be read:");
                Console.WriteLine (e.Message);
            }
            Console.ReadKey ();
        }
    }
}

When you compile and execute the above program, it will display the contents of the file.

StreamWriter class

StreamWriter class inherits from the abstract class TextWriter, represents the writer writes a series of characters.

The following table lists some of theStreamWriter class commonly used methods:

序号方法 & 描述
1public override void Close()
关闭当前的 StreamWriter 对象和基础流。
2public override void Flush()
清理当前编写器的所有缓冲区,使得所有缓冲数据写入基础流。
3public virtual void Write(bool value)
把一个布尔值的文本表示形式写入到文本字符串或流。(继承自 TextWriter。)
4public override void Write( char value )
把一个字符写入到流。
5public virtual void Write( decimal value )
把一个十进制值的文本表示形式写入到文本字符串或流。
6public virtual void Write( double value )
把一个 8 字节浮点值的文本表示形式写入到文本字符串或流。
7public virtual void Write( int value )
把一个 4 字节有符号整数的文本表示形式写入到文本字符串或流。
8public override void Write( string value )
把一个字符串写入到流。
9public virtual void WriteLine()
把行结束符写入到文本字符串或流。

For a complete list, please visit the Microsoft C # documents.

Examples

The following example demonstrates the use of the StreamWriter class to write data to a text file:

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 display each line string line = "" from the file;
            using (StreamReader sr = new StreamReader ( "names.txt"))
            {
                while ((line = sr.ReadLine ())! = null)
                {
                    Console.WriteLine (line);
                }
            }
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Zara Ali
Nuha Ali

Input and Output C # file Input and Output C # file