笔记8:vb.net的二进制数据流BinaryReader、BinaryWriter



           前面的FileStream、BufferedStream、MemoryStream类都是派生于Stream类。


           二进制数据流不同,它并不派生于Stream,而是直接派生于System.Object类。

           因此,天生不足,它只有串接到Stream类或派生类中进行操作,即二进制数据流的构造都要用Stream类。


           二进制数据流有两个类:BinaryReader和BinaryWriter


           它们有针对特定数据类型的专门读取方法。



一、BinaryReader类

      1、创建BinaryReader对象。

            BinaryReader本身不能创建对象,必须串接到Stream类型的数据流对象,才能进行创建读取相关操作。

    '基于所指定的流和特定的 UTF-8 编码,初始化 BinaryReader 类的新实例
    Public Sub New(input As Stream)

     '基于所指定的流和特定的字符编码,初始化 BinaryReader 类的新实例
    Public Sub New(input As Stream, encoding As Encoding)

        参数Stream指明的流的属性不能与BinaryReader冲突,比如此时是读,则流中应有读的特性。



      2、Read读取二进制数值

            每读一次,自动移动位置。

    '字符为单位,返回下一位读取的字符,位置下移一位。若是末尾返回-1
    Public Overridable Function Read() As Integer


        '以字节为单位读取,index缓冲区buffer的起始点,count长度。返回值:读取的个数
    Public Overridable Function Read(buffer As Byte(), index As Integer, count As Integer) As Integer


        '以字符为单位读取,
    Public Overridable Function Read(buffer As Char(), index As Integer, count As Integer) As Integer

          读取例子:

Imports System.IO
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fs As New FileStream("D:\11.txt", FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim br As New IO.BinaryReader(fs)
        TextBox1.Text = br.Read() '字符1,返回的是49(ASC码)
        TextBox1.Text &= Chr(br.Read()) '字符2,返回的是50(ASC码),这里chr将ASC转为字符
        br.Close()
        fs.Close()
    End Sub
End Class
         


        3、读取指定数据类型

             (1)读取字节

'从当前流中读取下一个字节,并使流的当前位置提升 1 个字节
Public Overridable Function ReadByte() As Byte    


'从当前流中读取指定的字节数以写入字节数组中,并将当前位置前移相应的字节数
Public Overridable Function ReadBytes(count As Integer) As Byte()
                  注意:如果数据位于数据流末尾将发生异常(下同)

              




             (2)读取整形:Short、Integer、Long

                   读取的本质都会转为.net framework的类型即:int16、int32、int64。因为他们占的字节分别是2,4,8

'从当前流中读取 2 字节有符号整数,并使流的当前位置提升 2 个字节
Public Overridable Function ReadInt16 As Short
 

'从当前流中读取 4 字节有符号整数,并使流的当前位置提升 4 个字节。
Public Overridable Function ReadInt32 As Integer    


    
'从当前流中读取 8 字节有符号整数,并使流的当前位置向前移动 8 个字节。
Public Overridable Function ReadInt64 As Long 
              
             咋一看,怎么不一样,把它转化成16进制看一下34 33 32  31  实际上就是字符4 3  2  1 的ASC码。
            注意:在读取对应数量字节时,若数据长度不足(如到了文件末),将发生错误。比如,离文件末还有2个字节时,读取为int32(4字节)将出错。




           (3)读取浮点型Decimal、Double、Single

'从当前流中读取十进制数值,并将该流的当前位置提升十六个字节。
Public Overridable Function ReadDecimal As Decimal
 

'从当前流中读取 8 字节浮点值,并使流的当前位置提升 8 个字节
Public Overridable Function ReadDouble As Double   


    
'从当前流中读取 4 字节浮点值,并使流的当前位置提升 4 个字节。
Public Overridable Function ReadSingle As Single


                   

               (4)读取字符型Char、String

                         Char是2个字节的单一Unicode,字符数值范围0--255;

                         String是一个以上Unicode字符组成的连续字符串。


'从当前流中读取下一个字符,并根据所使用的 Encoding 和从流中读取的特定字符,提升流的当前位置。
Public Overridable Function ReadChar As Char


'从当前流中读取指定的字符数,并以字符数组的形式返回数据,然后根据所使用的 Encoding 和从流中读取的特定字符,将当前位置前移。
Public Overridable Function ReadChars(count As Integer) As Char()



'从当前流中读取一个字符串。 字符串有长度前缀,一次 7 位地被编码为整数。
Public Overridable Function ReadString As String


               (5)特殊的预读:PeekChar

                    前面的读取,都会向前移动对应的位置,但有时只想查看前面字符,而又不想移动当前文件流的位置。

                    可以用PeekChar

                    ‘返回下一个可用的字符,并且不提升字节或字符的位置

              Public Overridable Function PeekChar As Integer



二、BinaryWriter类


             与BinaryReader类似,也须串接到数据流上。

           1、创建对象:

                  Public  Sub  New(ByVal   output  As  Stream)


          2、写入

                支持各种类型,同BinaryReader一样:


'各种数据类型
Public Overridable Sub Write (value As Boolean )
Public Overridable Sub Write (value As Byte )
Public Overridable Sub Write (ch As Char )
Public Overridable Sub Write (chars As Char() )
Public Overridable Sub Write (value As Decimal)
'...........



'字节数组
Public Overridable Sub Write ( _
	buffer As Byte(), _
	index As Integer, _
	count As Integer _
)

'字符数组
Public Overridable Sub Write ( _
	chars As Char(), _
	index As Integer, _
	count As Integer _
)





  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值