當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET FileStream類代碼示例

本文整理匯總了VB.NET中System.IO.FileStream的典型用法代碼示例。如果您正苦於以下問題:VB.NET FileStream類的具體用法?VB.NET FileStream怎麽用?VB.NET FileStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了FileStream類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Test

' 導入命名空間
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Delete the file if it exists.
        If File.Exists(path) Then
            File.Delete(path)
        End If

        'Create the file.
        Dim fs As FileStream = File.Create(path)

        AddText(fs, "This is some text")
        AddText(fs, "This is some more text,")
        AddText(fs, Environment.NewLine & "and this is on a new line")
        AddText(fs, Environment.NewLine & Environment.NewLine)
        AddText(fs, "The following is a subset of characters:" & Environment.NewLine)

        Dim i As Integer

        For i = 1 To 120
            AddText(fs, Convert.ToChar(i).ToString())

        Next

        fs.Close()

        'Open the stream and read it back.
        fs = File.OpenRead(path)
        Dim b(1023) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop

        fs.Close()
    End Sub

    Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)
        fs.Write(info, 0, info.Length)
    End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:49,代碼來源:FileStream

示例2: MainWindow

' 導入命名空間
Imports System.IO
Imports System.Text

Class MainWindow
    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim uniencoding As UnicodeEncoding = New UnicodeEncoding()
        Dim filename As String = "c:\Users\exampleuser\Documents\userinputlog.txt"

        Dim result As Byte() = uniencoding.GetBytes(UserInput.Text)

        Using SourceStream As FileStream = File.Open(filename, FileMode.OpenOrCreate)
            SourceStream.Seek(0, SeekOrigin.End)
            Await SourceStream.WriteAsync(result, 0, result.Length)
        End Using
    End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:17,代碼來源:FileStream

示例3: New FileStream(String fileName, FileMode fm)

' 導入命名空間
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
開發者ID:VB程序員,項目名稱:System.IO,代碼行數:60,代碼來源:FileStream


注:本文中的System.IO.FileStream類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。