.NET 编码以及流操作 (2)
Encoding
Encoding类在System.Text名字空间中。
using System;
using
System.Text;
using
System.IO;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
string test = "This
is our test string.";
byte[] ascb;
byte[] unicb;
byte[] utfb;
ascb = Encoding.ASCII.GetBytes(test);
Console.WriteLine("ASCII
Encoding : {0}", ascb.Length);
DisplayArray(ascb);
unicb = Encoding.Unicode.GetBytes(test);
Console.WriteLine("Unicode
Encoding : {0}", unicb.Length);
DisplayArray(unicb);
utfb = Encoding.UTF8.GetBytes(test);
Console.WriteLine("UTF8
Encoding : {0}", utfb.Length);
DisplayArray(utfb);
string unics = Encoding.Unicode.GetString(ascb);
Console.WriteLine(unics);
string ascs = Encoding.ASCII.GetString(unicb);
Console.WriteLine(ascs);
}
static void
DisplayArray(byte[] b)
{
for (int i = 0; i
< b.Length; i++)
{
Console.Write(b[i] + "
");
}
Console.WriteLine();
}
}
}
代码执行结果:
二进制文件
在System.IO名字空间中有用来处理二进制流的BinaryReader和BinaryWriter。BinaryReader默认使用UTF-8编码进行流的读取操作,当然用户也可以自己定义读取的编码方式。BinaryReader的Read方法可以进行字符的读取,如果到了文件的末尾,则返回-1。PeekChar方法可以返回流中的下一个字节。
指定使用ASCII编码对文件流进行读取:
Stream stream = new FileStream("D:\\test.txt",
FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(stream, Encoding.ASCII);
BinaryWriter的Write方法可以对流进行写操作,其中的Close和Flush方法可以对写得资源进行基本的管理操作。
using System;
using
System.Text;
using
System.IO;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
double angle,sinAngle;
Stream stream = new
FileStream("test.txt",
FileMode.Create, FileAccess.Write);
StreamWriter sw = new
StreamWriter(stream);
for (int i = 0; i
< 90; i++)
{
double angleRads = Math.PI
* i / 180;
sinAngle
= Math.Sin(angleRads);
sw.Write((double)i);
sw.Write('\n');
sw.Write(sinAngle);
}
sw.Close();
stream.Close();
}
}
}
TextReader
从TextReader派生来的类有StringReader以及StreamReader,使用StringReader的操作应该是在StringBuilder上进行的而不是string。
using System;
using
System.Text;
using
System.IO;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
Stream stream = new
FileStream("test.txt",
FileMode.Open, FileAccess.Read);
StreamReader sr = new
StreamReader(stream);
string data;
int line = 0;
while ((data = sr.ReadLine()) != null)
{
Console.WriteLine("Line{0}:{1}:Position={2}",
++line, data, sr.BaseStream.Position);
}
//now we return to the start of the file
sr.BaseStream.Seek(0,
SeekOrigin.Begin);
Console.WriteLine("*
Reading entire file using ReadToEnd \n" + sr.ReadToEnd());
sr.Close();
stream.Close();
}
}
}
TextWriter
StringWriter和StreamWriter的基类,StringWriter的使用基于StringBuilder而不是string。
using System;
using
System.Text;
using
System.IO;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
Stream stream = new
FileStream("test.txt",
FileMode.Create, FileAccess.Write);
StreamWriter sw = new
StreamWriter(stream);
Console.WriteLine("Encoding
type:" + sw.Encoding.ToString());
Console.WriteLine("Format
Provider:" + sw.FormatProvider.ToString());
sw.WriteLine("Today is {0}.", DateTime.Today.DayOfWeek);
sw.WriteLine("Today we will mostly be using StreamWriter.");
for (int i = 0; i
< 5; i++)
{
sw.WriteLine("Value {0}, its square is {1}", i, i *
i);
}
sw.Write("Arrays can be written:");
char[] myArray = new char[] { 'a', 'r', 'r', 'a', 'y' };
sw.Write(myArray);
sw.WriteLine("\r\nAnd parts of arrays can be written");
sw.Write(myArray,
0, 3);
sw.Close();
stream.Close();
}
}
}
执行结果: