C# 从文件读取数据块到字节数组
本文关键字:到字节 数组 数据 读取 文件 | 更新日期: 2023-09-27 18:30:43
我有一个程序,它有一个字节数组,其中包含一些数据和一个写入这些数据的文件。
byte[] bytes = new byte[16];
string path = "Dir/File.dat";
每次当"bytes"数组中的数据发生变化时,所有16个字节都会写入文件:
using (var stream = new FileStream(path, FileMode.Append))
{
stream.Write(bytes, 0, bytes.Length);
}
问题:
如何从此文件中读取数据?(像这样)
byte[] read_bytes = new byte[16];
while (not end of file yet) {
read a block of 16 bytes from file and put this data into "read_bytes"
MessageBox.Show(read_bytes[0]);
}
Filestream
将是您的情况的完美选择:
FileStream stream = new FileStream("Dir'File.dat", FileMode.Open, FileAccess.Read);
byte[] block = new byte[16];
while (stream.Read(block, 0, 16) > 0) { //as long as this does not return 0, the data in the file hasn't been completely read
//Print/do anything you want with [block], your 16 bytes data are there
}
如果没有更多数据,它的Read
方法将返回 0。这就是您知道文件已结束的方式
示例输出(每个字节更改为其十六进制字符串表示形式):
[2016-01-18 05:35:52.827 UTC] 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
[2016-01-18 05:35:52.829 UTC] 00 00 01 4E 00 00 00 51 08 02 00 00 00 32 C6 D8
[2016-01-18 05:35:52.829 UTC] C4 00 00 00 01 73 52 47 42 00 AE CE 1C E9 00 00
[2016-01-18 05:35:52.830 UTC] 00 04 67 41 4D 41 00 00 B1 8F 0B FC 61 05 00 00
[2016-01-18 05:35:52.830 UTC] 00 09 70 48 59 73 00 00 0E C3 00 00 0E C3 01 C7
[2016-01-18 05:35:52.830 UTC] 6F A8 64 00 00 02 F4 49 44 41 54 78 5E ED D7 3D
[2016-01-18 05:35:52.831 UTC] 4E 1B 41 18 80 E1 BD 13 92 25 EE E2 62 0B 0E 11
[2016-01-18 05:35:52.831 UTC] A5 A2 73 C5 29 28 4D C7 19 68 49 C9 09 E8 49 95
[2016-01-18 05:35:52.831 UTC] 22 45 32 FB EB D5 1A C4 2C C4 64 C7 DF F3 68 8A
[2016-01-18 05:35:52.832 UTC] F1 B7 B3 48 20 5E AF 5D BD 00 01 48 1D 42 90 3A
[2016-01-18 05:35:52.832 UTC] 84 20 75 08 41 EA 10 82 D4 21 04 A9 43 08 52 87
[2016-01-18 05:35:52.832 UTC] 10 A4 0E 21 48 1D 42 90 3A 84 20 75 08 41 EA 10
[2016-01-18 05:35:52.833 UTC] 82 D4 21 04 A9 43 08 52 87 10 A4 0E 21 48 1D 42
[2016-01-18 05:35:52.833 UTC] 90 3A 84 F0 7E EA D5 B7 A7 73 5D FD 6F 08 01 64
[2016-01-18 05:35:52.833 UTC] A5 FE F8 FC EB F9 E7 EF 33 5B 52 27 94 AC D4 67
[2016-01-18 05:35:52.833 UTC] 91 9C C7 92 3A A1 48 1D 42 90 3A 84 20 75 08 E1
[2016-01-18 05:35:52.834 UTC] 34 A9 FF B8 B9 D8 DC 3C CC 86 FF 7A DD D6 D5 C5
[2016-01-18 05:35:52.834 UTC] EE E3 6F 43 52 27 94 FF 97 FA 78 E6 63 EF 0B E9
[2016-01-18 05:35:52.834 UTC] AE 4A EA 90 AB D0 D4 9F AE 37 97 DB FA 52 EA 90
[2016-01-18 05:35:52.835 UTC] E9 74 A9 5F 6D 37 55 EB EA B6 9F 0C 3D 4F AF 1E
[2016-01-18 05:35:52.835 UTC] 36 43 F6 ED AB E9 5D D7 75 3F DA DE 0D 3F FF EE
[2016-01-18 05:35:52.835 UTC] AA AA EF 1F 76 E7 99 FA 77 B2 F5 7F 32 32 9C 2C
[2016-01-18 05:35:52.836 UTC] F5 A1 CC 14 64 CA F2 28 F5 9B 87 71 72 B8 94 9E
[2016-01-18 05:35:52.836 UTC] D5 47 77 8D 85 1F 8E DD 6F DB 37 82 33 4E FD 0F
[2016-01-18 05:35:52.836 UTC] 19 A4 BE C8 E9 3F C0 37 B9 5E DD CE 26 69 3F 4E
[2016-01-18 05:35:52.837 UTC] 8E 37 C7 67 26 57 C7 C2 A5 1E 5C 49 A9 3F EE 36
[2016-01-18 05:35:52.837 UTC] 9B DD 63 FF 62 E2 AD F9 D4 FC CC BE AE EA 7D BF
[2016-01-18 05:35:52.838 UTC] 5F E0 4B 52 7F 35 DA 71 72 BC 39 3E 73 B8 DA 3C
[2016-01-18 05:35:52.844 UTC] F9 A7 3E 5C BB D4 4B 17 2F F5 B4 EB FE EB 57 95
[2016-01-18 05:35:52.845 UTC] 7A F7 65 7B FC 28 3E 7C EA 6E AE A6 6F DA D3 8C
[2016-01-18 05:35:52.845 UTC] 0F 3D BF F6 01 7E 9E 7A BB 6F 97 A7 7A 70 9E EA
[2016-01-18 05:35:52.846 UTC] 8B 9C F0 BB 7A 6F E8 B3 A9 B7 9F 5C B6 D1 A6 F8
[2016-01-18 05:35:52.846 UTC] 93 D4 FF B8 99 DC D8 DD 25 75 DE 56 62 EA 29 D3
[2016-01-18 05:35:52.847 UTC] 51 D3 6B 33 AF EB D9 B3 3A 0D BB 41 D5 DE B3 EA
[2016-01-18 05:35:52.847 UTC] D4 4B 58 52 2F 5D D9 4F F5 6E D2 56 DD 85 DB 6C
[2016-01-18 05:35:52.848 UTC] 87 FA FB A3 E3 19 A9 7F 66 49 BD 74 45 A6 DE 04
[2016-01-18 05:35:52.848 UTC] 3D 98 65 DC 5C AA F7 D3 03 8D CD 6E 2F F5 CF 2D
[2016-01-18 05:35:52.849 UTC] A9 97 AE C0 D4 53 A5 43 B5 DD 64 96 7A 37 99 95
[2016-01-18 05:35:52.849 UTC] 3C 3D D3 90 FA C2 25 F5 D2 95 99 FA 50 69 57 FD
[2016-01-18 05:35:52.850 UTC] 24 EC 61 7B 34 EA EF 1D 49 7D E1 92 7A E9 0A 4C
[2016-01-18 05:35:52.851 UTC] BD 8D 77 D4 A7 3E 38 F4 9C 62 9E 8C BE 32 F5 73
[2016-01-18 05:35:52.851 UTC] 5D FD 6F B8 32 52 CF 54 52 EA 2B F0 7E EA 7C 31
[2016-01-18 05:35:52.852 UTC] A9 67 92 FA 22 52 5F 1D A9 67 92 FA 22 52 5F 1D
[2016-01-18 05:35:52.852 UTC] A9 67 92 FA 22 52 5F 1D A9 67 92 FA 22 52 5F 1D
[2016-01-18 05:35:52.853 UTC] A9 67 92 FA 22 52 5F 1D A9 67 92 FA 22 52 5F 1D
[2016-01-18 05:35:52.853 UTC] A9 67 92 FA 22 52 5F 9D F4 1F 4C A6 FE 4F 46 06
[2016-01-18 05:35:52.854 UTC] A9 43 08 52 87 10 A4 0E 21 48 1D 42 90 3A 84 20
[2016-01-18 05:35:52.855 UTC] 75 08 41 EA 10 82 D4 21 04 A9 43 08 52 87 10 A4
[2016-01-18 05:35:52.856 UTC] 0E 21 48 1D 42 90 3A 84 20 75 08 41 EA 10 82 D4
[2016-01-18 05:35:52.856 UTC] 21 04 A9 43 08 52 87 10 A4 0E 21 48 1D 42 90 3A
[2016-01-18 05:35:52.857 UTC] 84 20 75 08 41 EA 10 82 D4 21 04 A9 43 08 52 87
[2016-01-18 05:35:52.857 UTC] 10 A4 0E 21 48 1D 42 90 3A 84 20 75 08 41 EA 10
[2016-01-18 05:35:52.862 UTC] 82 D4 21 04 A9 43 00 2F 2F 7F 01 43 36 3E CD D7
[2016-01-18 05:35:52.862 UTC] C4 27 55 00 00 00 00 49 45 4E 44 AE 42 60 82 D7
你试过使用 read_bytes = System.IO.ReadAllBytes(""Dir/File.dat")
吗?
编辑:如果太大,请使用 FileStream然后,您可以使用 ReadByte() 遍历文件的每个字节,并将它们排序为 16 个组。你也可以使用Stream.Read,它更健壮一些。你可能一次可以抓住 16 个。
您可以使用以下代码来实现您的目的
char[] read_bytes = new char[16];
using (var stream = new StreamReader("sample.txt"))
{
while (stream.Read(read_bytes, 0, read_bytes.Length) > 0)
{
Console.WriteLine(read_bytes);
}
}
它使用 StreamReader 类,以大小为 16 的块逐字节读取流。