对字节读写操作示例,来自微软

来源:互联网 发布:矩阵力学的主要创立者 编辑:程序博客网 时间:2024/06/05 15:42

对字节读写操作

            byte[] 字节 = new byte[200];            char[] 字符 = new Char[200];            try            {                FileStream 读取文件 = new FileStream("../../Program.cs", FileMode.Open);                读取文件.Seek(113, SeekOrigin.Begin);                读取文件.Read(字节, 0, 200);            }            catch (IOException e)            {                Console.WriteLine("IO异常已被抛出!");                Console.WriteLine(e.ToString());                Console.ReadKey();                return;            }            Decoder 编码 = Encoding.UTF8.GetDecoder();            编码.GetChars(字节, 0, 字节.Length, 字符, 0);            Console.WriteLine(字符);            byte[] 字节1;            char[] 字符1;            try            {                FileStream 写入文件 = new FileStream("温度.txt", FileMode.Create);                字符1 = "My pink half of the drainpipe.".ToCharArray();                字节1 = new byte[字符1.Length];                Encoder 编码1 = Encoding.UTF8.GetEncoder();                编码1.GetBytes(字符1, 0, 字符1.Length, 字节1, 0, true);                写入文件.Seek(0, SeekOrigin.Begin);                写入文件.Write(字节1, 0, 字节1.Length);            }            catch (IOException ex)            {                Console.WriteLine("IO异常已被抛出!");                Console.WriteLine(ex.ToString());                Console.ReadKey();                return;            }            try            {                FileStream 写入文件 = new FileStream("温度.txt", FileMode.OpenOrCreate);                StreamWriter 写入 = new StreamWriter(写入文件);                bool aj = true;                写入.WriteLine("hello to you.");                写入.WriteLine("it is now{0}and things are looking good.", DateTime.Now.ToLongDateString());                写入.Write("more than that.");                写入.Write("it's{0}that c# is fun.", aj);                写入.Close();            }            catch (IOException e)            {                Console.WriteLine("IO异常已被抛出!");                Console.WriteLine(e.ToString());                Console.ReadKey();                return;            }            try            {                FileStream 写入文件 = new FileStream("温度.txt", FileMode.Open);                StreamReader 写入 = new StreamReader(写入文件);                //string 内容 = 写入.ReadLine();                string 内容 = 写入.ReadToEnd();                Console.WriteLine(内容);                //while (内容 != null)                //{                //    Console.WriteLine(内容);                //    内容 = 写入.ReadLine();                //}                //int 内容值 = 写入.Read();                //while (内容值 != -1)                //{                //    Console.WriteLine(Convert.ToChar(内容值));                //    内容值 = 写入.Read();                //}                写入.Close();            }            catch (IOException e)            {                Console.WriteLine("IO异常已被抛出!");                Console.WriteLine(e.ToString());                Console.ReadKey();                return;            }

0 0