VB.Net/File Directory/Binary File Read — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:45, 26 мая 2010
Содержание
Check Files Are Identical
Public Class MainClass
Public Shared Function CompareFiles(ByVal File1 As String, ByVal File2 As String) As Boolean
Dim blnIdentical As Boolean = True
Dim objFS1 As System.IO.FileStream = New System.IO.FileStream(File1, System.IO.FileMode.Open)
Dim objFS2 As System.IO.FileStream = New System.IO.FileStream(File2, System.IO.FileMode.Open)
If (objFS1.Length <> objFS2.Length) Then
blnIdentical = False
Else
Dim intByteF1 As Integer
Dim intByteF2 As Integer
Do
intByteF1 = objFS1.ReadByte()
intByteF2 = objFS2.ReadByte()
If intByteF1 <> intByteF2 Then
blnIdentical = False
Exit Do
End If
Loop While (intByteF1 <> -1)
End If
objFS1.Close()
objFS2.Close()
Return blnIdentical
End Function
Public Shared Sub Main()
If CompareFiles("1.jpg", "2.bmp") Then
System.Console.WriteLine("Files are identical!")
Else
System.Console.WriteLine("Files do not match!")
End If
End Sub
End Class
Read and Write Binary file: int, string
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
Read from a binary file
Imports System
Imports System.IO
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim emp As New Employee
Dim file_num As Integer = FreeFile()
FileOpen(file_num, "MYFILE.DAT", OpenMode.Random, _
OpenAccess.ReadWrite, OpenShare.Shared, _
Len(emp))
FilePut(file_num, New Employee(1, "A", "A"))
FilePut(file_num, New Employee(2, "B", "B"))
FilePut(file_num, New Employee(3, "C", "C"))
FilePut(file_num, New Employee(4, "D", "D"))
FilePut(file_num, New Employee(5, "E", "E"))
FilePut(file_num, New Employee(6, "F", "F"))
Dim obj As ValueType = DirectCast(emp, ValueType)
For Each i As Integer In New Integer() {3, 1, 5, 2, 6}
FileGet(file_num, obj, i)
emp = DirectCast(obj, Employee)
Console.WriteLine(emp.ToString())
Next i
" Close the file.
FileClose(file_num)
End Sub
End Class
Public Structure Employee
Public ID As Integer
<VBFixedString(15)> Public FirstName As String
<VBFixedString(15)> Public LastName As String
Public Sub New(ByVal new_id As Integer, ByVal first_name As String, _
ByVal last_name As String)
ID = new_id
FirstName = first_name
LastName = last_name
End Sub
Public Overrides Function ToString() As String
Return ID & ": " & FirstName & " " & LastName
End Function
End Structure
Reading a sequential-access file
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Public Class MainClass
Public Shared Sub Main()
Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim output As FileStream
Dim fileName As String = "test.dat"
Dim employee As Employee = New Employee("First Name", "Last Name")
Try
output = New FileStream(fileName,FileMode.OpenOrCreate, FileAccess.Write)
formatter.Serialize(output, employee)
output.Close()
Catch fileException As FileNotFoundException
Console.WriteLine("File Does Not Exits")
Catch serializableException As SerializationException
Console.WriteLine("Error Writing to File")
Catch formattingException As FormatException
Console.WriteLine("Invalid Format")
Catch e As IOException
Console.WriteLine("Cannot close file")
End Try
Dim input As FileStream
Dim reader As BinaryFormatter = New BinaryFormatter()
input = New FileStream(fileName, FileMode.Open,FileAccess.Read)
Try
Dim em As Employee = CType(reader.Deserialize(input), Employee)
Console.WriteLine(em)
Catch serializableException As SerializationException
input.Close()
Console.WriteLine("No more records in file")
End Try
End Sub
End Class
<Serializable()> Public Class Employee
Private firstName, lastName As String
Public Sub New(ByVal first As String, ByVal last As String)
firstName = first
lastName = last
End Sub
Public Overrides Function ToString() As String
Return firstName & " " & lastName
End Function
End Class