C# Reading and Writing Text Files
StreamReaderandStreamWriterThe class is used for reading and writing data of text files. These classes inherit from the abstract base class Stream, which supports byte reading and writing of file streams.
StreamReader Class
StreamReaderThe class inherits from the abstract base class TextReader, representing a reader that reads a sequence of characters.
The table below listsStreamReaderSome commonly used in the classMethods:
| Serial Number | Methods & Description |
|---|---|
| 1 | public override void Close() Close the StreamReader object and the underlying stream, and release any system resources associated with the reader. |
| 2 | public override int Peek() Returns the next available character, but does not consume it. |
| 3 | public override int Read() Reads the next character from the input stream and advances the character position by one character. |
To view the complete list of methods, please visit Microsoft's C# documentation.
Example
The following example demonstrates reading a file named Jamaica.txt. The file is as follows:
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
{
// 创建一个 StreamReader 的实例来读取文件
// using 语句也能关闭 StreamReader
using (StreamReader sr = new StreamReader("c:/jamaica.txt", Encoding.UTF8))
{
string line;
// 从文件读取并显示行,直到文件的末尾
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// 向用户显示出错消息
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
StreamWriterThe class inherits from the abstract class TextWriter, representing a writer that writes a sequence of characters.
The table below listsStreamWriterSome commonly used in the classMethods:
| Serial Number | Methods & Description |
|---|---|
| 1 | public override void Close() Closes the current StreamWriter object and the underlying stream. |
| 2 | public override void Flush() Clears all buffers of the current writer, causing all buffered data to be written to the underlying stream. |
| 3 | public virtual void Write(bool value) Writes the text representation of a Boolean value to the text string or stream. (Inherited from TextWriter.) |
| 4 | public override void Write(
char value
)
Write a character to the stream. |
| 5 | public virtual void Write(
decimal value
)
Writes the text representation of a decimal value to the text string or stream. |
| 6 | public virtual void Write(
double value
)
Writes the text representation of an 8-byte floating-point value to the text string or stream. |
| 7 | public virtual void Write(
int value
)
Writes the text representation of a 4-byte signed integer to the text string or stream. |
| 8 | public override void Write(
string value
)
Write a string to the stream. |
| 9 | public virtual void WriteLine() Writes the line terminator to the text string or stream. |
To view the complete list of methods, please visit Microsoft's C# documentation.
Example
The following example demonstrates writing text data to a file using the StreamWriter class:
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);
}
}
// 从文件中读取并显示每行
string line = "";
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 Aliother extensions