VB.Net by API/System.IO/BinaryReader — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:50, 26 мая 2010

BinaryReader.Read

  
Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim fs As System.IO.FileStream
        Dim r As System.IO.BinaryReader
        Dim buffer(100) As Char
        Dim mylength As Long
        fs = New System.IO.FileStream("test.txt", IO.FileMode.OpenOrCreate)
        r = New System.IO.BinaryReader(fs)
        mylength = fs.Length
        If mylength > 100 Then
            mylength = 100
        End If
        r.Read(buffer, 0, mylength)
        Console.WriteLine(buffer)
        r.Close()
        fs.Close()
    End Sub
End Class


BinaryReader.ReadDouble()

  
Imports System.IO
Module Module1
    Sub Main()
        Dim Stream As FileStream
        Try
            Stream = New FileStream("test.dat", FileMode.Create)
        Catch E As Exception
            Console.WriteLine("Error creating test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        Dim BinaryStream As New BinaryWriter(Stream)
        Dim Age As Integer = 21
        Dim Salary As Double = 100000.0
        Dim Name As String = "Joe"
        Try
            BinaryStream.Write(Age)
            BinaryStream.Write(Salary)
            BinaryStream.Write(Name)
            BinaryStream.Close()
            Console.WriteLine("Data written to test.Dat")
        Catch E As Exception
            Console.WriteLine("Error writing to test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        
        "Read 
        
        Try
            Stream = New FileStream("test.dat", FileMode.Open)
        Catch E As Exception
            Console.WriteLine("Error opening test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        Dim BinaryStreamReader As New BinaryReader(Stream)
        Try
            Age = BinaryStreamReader.ReadInt32()
            Salary = BinaryStreamReader.ReadDouble()
            Name = BinaryStreamReader.ReadString()
            BinaryStreamReader.Close()
            Console.WriteLine("Age: {0}", Age)
            Console.WriteLine("Salary: {0}", Salary)
            Console.WriteLine("Name: {0}", Name)
        Catch E As Exception
            Console.WriteLine("Error reading to test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
    End Sub
End Module


BinaryReader.ReadInt32()

  
Imports System.IO
Module Module1
    Sub Main()
        Dim Stream As FileStream
        Try
            Stream = New FileStream("test.dat", FileMode.Create)
        Catch E As Exception
            Console.WriteLine("Error creating test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        Dim BinaryStream As New BinaryWriter(Stream)
        Dim Age As Integer = 21
        Dim Salary As Double = 100000.0
        Dim Name As String = "Joe"
        Try
            BinaryStream.Write(Age)
            BinaryStream.Write(Salary)
            BinaryStream.Write(Name)
            BinaryStream.Close()
            Console.WriteLine("Data written to test.Dat")
        Catch E As Exception
            Console.WriteLine("Error writing to test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        
        "Read 
        
        Try
            Stream = New FileStream("test.dat", FileMode.Open)
        Catch E As Exception
            Console.WriteLine("Error opening test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        Dim BinaryStreamReader As New BinaryReader(Stream)
        Try
            Age = BinaryStreamReader.ReadInt32()
            Salary = BinaryStreamReader.ReadDouble()
            Name = BinaryStreamReader.ReadString()
            BinaryStreamReader.Close()
            Console.WriteLine("Age: {0}", Age)
            Console.WriteLine("Salary: {0}", Salary)
            Console.WriteLine("Name: {0}", Name)
        Catch E As Exception
            Console.WriteLine("Error reading to test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
    End Sub
End Module


BinaryReader.ReadString()

  

Imports System.IO
Module Module1
    Sub Main()
        Dim Stream As FileStream
        Try
            Stream = New FileStream("test.dat", FileMode.Create)
        Catch E As Exception
            Console.WriteLine("Error creating test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        Dim BinaryStream As New BinaryWriter(Stream)
        Dim Age As Integer = 21
        Dim Salary As Double = 100000.0
        Dim Name As String = "Joe"
        Try
            BinaryStream.Write(Age)
            BinaryStream.Write(Salary)
            BinaryStream.Write(Name)
            BinaryStream.Close()
            Console.WriteLine("Data written to test.Dat")
        Catch E As Exception
            Console.WriteLine("Error writing to test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        
        "Read 
        
        Try
            Stream = New FileStream("test.dat", FileMode.Open)
        Catch E As Exception
            Console.WriteLine("Error opening test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
        Dim BinaryStreamReader As New BinaryReader(Stream)
        Try
            Age = BinaryStreamReader.ReadInt32()
            Salary = BinaryStreamReader.ReadDouble()
            Name = BinaryStreamReader.ReadString()
            BinaryStreamReader.Close()
            Console.WriteLine("Age: {0}", Age)
            Console.WriteLine("Salary: {0}", Salary)
            Console.WriteLine("Name: {0}", Name)
        Catch E As Exception
            Console.WriteLine("Error reading to test.Dat")
            Console.WriteLine("Error {0}", E.Message)
        End Try
    End Sub
End Module