VB.Net Tutorial/Stream File/MemoryStream

Материал из VB Эксперт
Версия от 12:56, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Use MemoryStream to read a file

Imports System.IO
Imports System.Text
Imports System.Threading
        
Public Class Tester
    Public Shared Sub Main
        Dim bteRead() As Byte
        Dim bteArray(256) As Byte
        Dim myMemortStream As MemoryStream
        Dim myFileStream As FileStream
        Try
            myFileStream = New FileStream("test.vb", FileMode.Open, FileAccess.Read)
            ReDim bteRead(myFileStream.Length)
            myFileStream.Read(bteRead, 0, myFileStream.Length)
            myMemortStream = New MemoryStream(256)
            myMemortStream.Write(bteRead, 128, 256)
            bteArray = myMemortStream.ToArray()
            Console.WriteLine(Encoding.ASCII.GetString(bteArray))
            myMemortStream.Close()
            myFileStream.Close()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
    
End Class

Use MemoryStream with BinaryWriter and BinaryReader

Imports System.IO
public class Test
   public Shared Sub Main
        Dim memory_stream As New MemoryStream()
        Dim binary_writer As New BinaryWriter(memory_stream)
        binary_writer.Write("aaa.")
        Dim binary_reader As New BinaryReader(memory_stream)
        memory_stream.Seek(0, SeekOrigin.Begin)
        Console.WriteLine(binary_reader.ReadString())
        binary_reader.Close()

   End Sub
End class
aaa.

Use MemoryStream with StreamWriter and StreamReader

Imports System.IO
public class Test
   public Shared Sub Main
        Dim memory_stream As New MemoryStream()
        Dim stream_writer As New StreamWriter(memory_stream)
        stream_writer.Write("aaa.")
        stream_writer.Flush()
        Dim stream_reader As New StreamReader(memory_stream)
        memory_stream.Seek(0, SeekOrigin.Begin)
        Console.WriteLine(stream_reader.ReadToEnd())
        stream_reader.Close()
   End Sub
End class
aaa.