摘要:
StreamWrite和StreamReader 读写操作using System;
using System.IO;
using System.Text;
namespace StreamWrite和StreamReader
{
class Program
{
StreamWrite和StreamReader 读写操作using System;
using System.IO;
using System.Text;
namespace StreamWrite和StreamReader
{
class Program
{
static void Main(string[] args)
{
using (StreamWriter sw = new StreamWriter(@"D:\Users\Desktop\小道博客.txt", true, Encoding.UTF8))//写入,追加文本(true)
{
sw.Write("站名:");//写入
sw.WriteLine("小道博客");//写入行
sw.Write("网址:");//写入
sw.WriteLine("http://www.daobk.com");//写入行
Console.WriteLine("写入成功!按任意键读取.");//提示
}
using (StreamReader sr = new StreamReader(@"D:\Users\Desktop\小道博客.txt", Encoding.UTF8))//读取
{
//第一种读取方法
while (!sr.EndOfStream)//获取一个值,该值表示当前的流位置是否在流的末尾。(如果当前流位置位于流的末尾,则为 true;否则为 false。)取非
{
string str = sr.ReadLine();//从当前流中读取一行字符并将数据作为字符串返回。
Console.WriteLine(str);//输出
}
//第二种读取方法
//string s = null;//声明字符串 为null
//while ((s = sr.ReadLine()) != null)// sr.ReadLine() 返回结果 输入流中的下一行;如果到达了输入流的末尾,则为 null。,判断读取数据是否不为空,不为空则为真 继续读取
//{
// Console.WriteLine(s);//输出
//}
Console.WriteLine("读取完成!");//提示
}
Console.ReadKey();//按任意键
}
}
}
输出结果: