您的位置:首页 > 编程语言 > C#

C#操作文本读写流--- StreamWritter and StreamReader

2007-07-09 11:17 507 查看
.Net 为我们对流进行了封装,所以在我们用c#进行流操作时,可以直接使用封装好的高级流StreamReader和StreamWritter来进行文本的读写操作,不用再用二进制进行文本的读写操作,这样方便快捷,但是运用二进制进行文本操作是永远可行的,毕竟文本流的本质就是二进制流。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO; //Stream操作必须要添加的域名空间

namespace Stream
{
class Program
{
static void Main(string[] args)
{
string path = @"E:StreamText.txt";
StreamManipulation.Write(path);
StreamManipulation.Read(path);
}
}

class StreamManipulation
{
public static void Write(string path)
{
StreamWriter sw = new StreamWriter(path, false);
sw.WriteLine("Welcome to the stream world!");
sw.WriteLine("Come On!");
sw.Close();
}
public static void Read(string path)
{
StreamReader sr = new StreamReader(path);
string s;
while (!sr.EndOfStream)
{
s = sr.ReadLine();
Console.WriteLine(s);
}
Console.Read();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: