字节数组与文件之间的相互转换的工具

2021-04-30 19:29

阅读:709

标签:pat   测试   return   path   读取   解析   writer   lin   txt   

工具代码:

 1 using System;
 2 using System.IO;
 3 
 4 /// 
 5 /// 字节数组与文件之间的相互转换的接口
 6 /// 
 7 public interface IConvertBetweenBytesAndFile
 8 {
 9     /// 
10     /// 将字节数组写入文件
11     /// 
12     /// 目标文件
13     /// 待写入的字节数组
14     void WriteBytesToFile(string filePath, byte[] bytes);
15     /// 
16     /// 从文件读取字节数组
17     /// 
18     /// 
19     /// 
20     byte[] ReadBytesFromFile(string filePath);
21 }
22 
23 /// 
24 /// 方式一
25 /// 
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 /// 
44 /// 方案二
45 /// 
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 /// 
 4 /// 定义序列化的对象
 5 /// 
 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     /// 
17     /// 编码
18     /// 
19     /// 
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     /// 
34     /// 解码
35     /// 
36     /// 
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 }

运行结果:

技术图片技术图片

字节数组与文件之间的相互转换的工具

标签:pat   测试   return   path   读取   解析   writer   lin   txt   

原文地址:https://www.cnblogs.com/luguoshuai/p/13226524.html


评论


亲,登录后才可以留言!