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

本文介绍了如何使用C#中的Stream操作来进行文本文件的读写。通过具体的代码示例展示了如何利用StreamWriter和StreamReader来实现这一过程,使得文本操作更加简便快捷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     .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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值