post

Stream与String的互相转换

很简单的方法,将Stream流转换为字符串,或者将字符串转换为Stream流.

using System;
using System.IO;
using System.Text;
namespace CSharpConvertString2Stream
{
class Program
{
static void Main( string[] args )
{
string str = "Testing 1-2-3"; //convert string 2 stream
byte[] array = Encoding.ASCII.GetBytes(str);
MemoryStream stream = new MemoryStream(array); //convert stream 2 string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
Console.WriteLine(text);
Console.ReadLine();
}
}
}

Speak Your Mind

*

· 711 次浏览