以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]关于二进制读取问题  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=77404)

--  作者:jspta
--  发布时间:2015/11/17 18:55:00
--  [求助]关于二进制读取问题
使用FileStream读取文件后,在用BinaryReader读取
文件流开头为 00 07 00 05 ……
使用BinaryReader.ReadUInt16 读取出来是 07 00 变成了 256 * 7 而我实际上只想得到 7(4个字节一组)。

另外在文件较大的时候,对二进制流进行循环,大约60M的数据,循环一次需要近11秒,这个显然太慢了,希望在1秒作用。
有没有办法快速查找到某字节或者分块循环。(我不知道需要的字节在哪里,而且因为整个流是连续不断,每个块的结束就是另一个快的开始,所以分割会不会出现错误?)

代码如下,不涉及其它代码
Dim dtm As Date = Date.now
Dim strFilePath As String = ""
Dim fs As New System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
Dim br As New System.IO.BinaryReader(fs)
Dim nFS As Integer = fs.length
Dim n As Integer = 0
Do While n<fs.length
    If nfs -n >= 4 Then
        Dim nlength As Integer = br.readbyte * 256 + br.readbyte
        Dim nString As Integer = br.readbyte * 256 + br.readbyte
        Select Case nlength
            Case 0
                n = n + 4
            Case 1,2,3,4
                n = n + nlength 
            Case Else
                br.readbytes(nLength-4)
                n = n + nlength 
        End Select 
    Else
        n = fs.length
    End If
Loop
Output.Show((Date.now - dtm).Totalseconds)


--  作者:大红袍
--  发布时间:2015/11/17 19:23:00
--  

呃,你想获取多少都可以的啊

 

Dim byteData(3) As Byte

Dim strFilePath As String = "d:\\test.txt"

Dim fs As New System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
fs.Read(byteData, 0, byteData.Length)

msgbox(byteData(0))
msgbox(byteData(1))
msgbox(byteData(2))
msgbox(byteData(3))