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

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

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

BinaryWriter.Seek

   
Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim fs As System.IO.FileStream
        Dim w As System.IO.BinaryWriter
        Dim buffer As String
        Dim c As Char
        c = Chr(9)
        fs = New System.IO.FileStream("test.txt", IO.FileMode.OpenOrCreate)
        w = New System.IO.BinaryWriter(fs)
        w.Seek(0, System.IO.SeekOrigin.Begin)
        w.Write("writing data via BinaryWriter")
        w.Close()
        fs.Close()
    End Sub
End Class


BinaryWriter.Write(Double d)

   

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


BinaryWriter.Write(Integer i)

   

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


BinaryWriter.Write(String s)

   

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


New BinaryWriter(FileStream aFileStream)

   
Imports System
Imports System.IO
Public Class MainClass
  Shared Sub Main()
    Dim aFileStream As FileStream
    Try
      aFileStream = New FileStream("test.txt", FileMode.OpenOrCreate,FileAccess.Write)
      Dim myBinaryWriter As New BinaryWriter(aFileStream)
      myBinaryWriter.Write("Hello world")
      myBinaryWriter.Write(1)
    Catch e As Exception
      Console.WriteLine(e.StackTrace)
    Finally
      If Not (aFileStream Is Nothing) Then aFileStream.Close()
    End Try
  End Sub
  
End Class