zoukankan      html  css  js  c++  java
  • 字节数组与文件之间的相互转换的工具

    工具代码:

     1 using System;
     2 using System.IO;
     3 
     4 /// <summary>
     5 /// 字节数组与文件之间的相互转换的接口
     6 /// </summary>
     7 public interface IConvertBetweenBytesAndFile
     8 {
     9     /// <summary>
    10     /// 将字节数组写入文件
    11     /// </summary>
    12     /// <param name="filePath">目标文件</param>
    13     /// <param name="bytes">待写入的字节数组</param>
    14     void WriteBytesToFile(string filePath, byte[] bytes);
    15     /// <summary>
    16     /// 从文件读取字节数组
    17     /// </summary>
    18     /// <param name="filePath"></param>
    19     /// <returns></returns>
    20     byte[] ReadBytesFromFile(string filePath);
    21 }
    22 
    23 /// <summary>
    24 /// 方式一
    25 /// </summary>
    26 public class ConvertBetweenBytesAndFile_ToolA : IConvertBetweenBytesAndFile
    27 {
    28     public byte[] ReadBytesFromFile(string filePath)
    29     {
    30         if (string.IsNullOrEmpty(filePath))
    31             return null;
    32         return File.ReadAllBytes(filePath);
    33     }
    34 
    35     public void WriteBytesToFile(string filePath, byte[] bytes)
    36     {
    37         if (string.IsNullOrEmpty(filePath))
    38             return;
    39         File.WriteAllBytes(filePath, bytes);
    40     }
    41 }
    42 
    43 /// <summary>
    44 /// 方案二
    45 /// </summary>
    46 public class ConvertBetweenBytesAndFile_ToolB : IConvertBetweenBytesAndFile
    47 {
    48 
    49     public void WriteBytesToFile(string filePath, byte[] bytes)
    50     {
    51         using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
    52         {
    53             fileStream.Write(bytes, 0, bytes.Length);
    54         }
    55     }
    56 
    57     public byte[] ReadBytesFromFile(string filePath)
    58     {
    59         using (FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read))
    60         {
    61             using (BinaryReader reader = new BinaryReader(fileStream))
    62             {
    63                 long length = fileStream.Length;
    64                 byte[] bytes = new byte[length];
    65                 reader.Read(bytes, 0, bytes.Length);
    66                 return bytes;
    67             }
    68         }
    69     }
    70 }

    定义可序列化的类:

     1 using System.IO;
     2 
     3 /// <summary>
     4 /// 定义序列化的对象
     5 /// </summary>
     6 public class People
     7 {
     8     public int age;
     9     public string name;
    10 
    11     public override string ToString()
    12     {
    13         return "Name = " + name + "; Age = " + age;
    14     }
    15 
    16     /// <summary>
    17     /// 编码
    18     /// </summary>
    19     /// <returns></returns>
    20     public byte[] DoEncode()
    21     {
    22         using (MemoryStream ms = new MemoryStream())
    23         {
    24             using (BinaryWriter writer = new BinaryWriter(ms))
    25             {
    26                 writer.Write(age);
    27                 writer.Write(name);
    28             }
    29             return ms.ToArray();
    30         }
    31     }
    32 
    33     /// <summary>
    34     /// 解码
    35     /// </summary>
    36     /// <param name="bytes"></param>
    37     public void DoDecode(byte[] bytes)
    38     {
    39         if (null == bytes)
    40         {
    41             return;
    42         }
    43 
    44         using (MemoryStream ms = new MemoryStream(bytes))
    45         {
    46             using (BinaryReader reader = new BinaryReader(ms))
    47             {
    48                 age = reader.ReadInt32();
    49                 name = reader.ReadString();
    50             }
    51         }
    52     }
    53 }

    测试:

     1 using System;
     2 
     3 namespace myMethod
     4 {
     5     class lgs
     6     {
     7         static void Main()
     8         {
     9             string pathA = @"D:Test.txt";
    10 
    11             //工具对象
    12             IConvertBetweenBytesAndFile toolA = new ConvertBetweenBytesAndFile_ToolA();
    13 
    14             byte[] bytes = null;
    15 
    16             //将对象写入文件
    17             People people = new People();
    18             people.age = 12;
    19             people.name = "Tom";
    20             bytes = people.DoEncode();
    21             toolA.WriteBytesToFile(pathA, bytes);
    22 
    23             //从文件中解析对象
    24             People p2 = new People();
    25             bytes = toolA.ReadBytesFromFile(pathA);
    26             p2.DoDecode(bytes);
    27 
    28             Console.WriteLine(p2.ToString());
    29 
    30             Console.ReadKey();
    31         }
    32     }
    33 }

    运行结果:

  • 相关阅读:
    保护你的网站数字证书私钥
    Web安全防御从WAF到应用网关
    微信公众号【网络安全生命周期】部分文章目录
    一个SQL Injection漏洞在SDL流程中的闯关历险记
    Web Vulnerability Scanner 破解
    Web安全实战演练1-4
    转:WebCruiser Web Vulnerability Scanner 3 测评
    WAVSEP在Linux系统中部署时常见错误
    css属性操作
    前端基础CSS规则
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/13226524.html
Copyright © 2011-2022 走看看