现在的位置: 首页 > 综合 > 正文

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

2013年09月09日 ⁄ 综合 ⁄ 共 1051字 ⁄ 字号 评论关闭

     .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();
        }
    }
}

抱歉!评论已关闭.