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

对字节读写操作

            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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值