1. 程式人生 > 實用技巧 >位元組陣列與檔案之間的相互轉換的工具

位元組陣列與檔案之間的相互轉換的工具

工具程式碼:

 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 }

執行結果: