本文整理汇总了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
示例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
示例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