VB.Net Tutorial/Stream File/StreamReader
Содержание
Check Exception in file reading
<source lang="vbnet">Imports System.IO Imports System public class Test
public Shared Sub Main Dim sr As IO.StreamReader Try sr = New IO.StreamReader("test.txt") Console.WriteLine(sr.ReadToEnd()) Catch ex As IO.FileNotFoundException Console.WriteLine("FileNotFoundException") Catch ex As IO.IOException Console.WriteLine("IOException") Catch ex As Exception Console.WriteLine("Error Loading File") Finally sr.Close() End Try End Sub
End class</source>
Hello world!
Create StreamReader from FileStream
<source lang="vbnet">Imports System.IO
Public Class Tester
Public Shared Sub Main Dim f As System.IO.FileStream Dim r As System.IO.StreamReader Dim mylength As Integer Dim i As Integer f = New System.IO.FileStream("test.txt", IO.FileMode.Open, IO.FileAccess.Read) r = New System.IO.StreamReader(f) Console.WriteLine(r.ReadToEnd()) f.Close() r.Close() End Sub
End Class</source>
Read all text in a text file by using StreamReader
<source lang="vbnet">Imports System.IO Module Module1
Sub Main() Dim SourceFile As StreamReader Try SourceFile = New StreamReader("test.txt") Console.WriteLine(SourceFile.ReadToEnd()) SourceFile.Close() Catch Except As Exception Console.WriteLine("Debug file does not yet exist") End Try End Sub
End Module</source>
<HTML> <HEAD> Java examples (example source code) Organized by topic </title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta http-equiv="content -style-type" content="text/css"/> <meta name="author" content="Demo Source and Support Ltd."/> <meta name="copyright" content="2006 Demo Source and Support Ltd."/> <meta name="description" CONTENT=" Java examples (example source code) Organized by topic " /> <meta name="keywords" CONTENT=" Java examples (example source code) Organized by topic "/> ...
Read text file to a char array
<source lang="vbnet">Imports System.IO
Public Class Tester
Public Shared Sub Main Dim f As System.IO.FileStream Dim r As System.IO.StreamReader Dim mylength As Integer Dim i As Integer Dim mybuffer(100) As Char f = New System.IO.FileStream("test.txt", IO.FileMode.Open, IO.FileAccess.Read) r = New System.IO.StreamReader(f) r.Read(mybuffer, 0, 100) Console.WriteLine(mybuffer) f.Close() r.Close() End Sub
End Class</source>
Read text file to the end
<source lang="vbnet">Imports System.IO Public Class Tester
Public Shared Sub Main Try Dim stream As StreamReader stream = New StreamReader("test.vb") Console.WriteLine(stream.ReadToEnd()) Catch exceptionCatch As IOException Console.WriteLine("FILE ERROR") End Try End Sub
End Class</source>
StreamReader: Peek
<source lang="vbnet">Imports System.IO Public Class Tester
Public Shared Sub Main Dim fsStream As New FileStream("test.txt", FileMode.Open, FileAccess.Read) Dim srReader As New StreamReader(fsStream) Try srReader.BaseStream.Seek(0, SeekOrigin.Begin) While srReader.Peek() > -1 Console.WriteLine(srReader.ReadLine()) End While srReader.Close() Catch ex As IOException Console.WriteLine(ex.Message) End Try End Sub
End Class</source>
StreamReader: read to a buffer
<source lang="vbnet">Imports System.IO
Public Class Tester
Public Shared Sub Main Dim f As System.IO.FileStream Dim r As System.IO.StreamReader Dim mylength As Integer Dim i As Integer Dim mybuffer(100) As Char f = New System.IO.FileStream("test.txt", IO.FileMode.Open, IO.FileAccess.Read) r = New System.IO.StreamReader(f) r.Read(mybuffer, 0, 100) Console.WriteLine(mybuffer) f.Close() r.Close() End Sub
End Class</source>
Use Finally clause to close a stream
<source lang="vbnet">Imports System.IO Imports System.Windows.Forms Module Module1
Sub Main() Dim FileDB As New OpenFileDialog() FileDB.Filter = "All files | *.* | Text files | *.txt" FileDB.FilterIndex = 2 FileDB.InitialDirectory = "C:\Temp" FileDB.AddExtension = True FileDB.DefaultExt = "txt" If (FileDB.ShowDialog() = DialogResult.OK) Then Dim SourceFile As StreamReader SourceFile = New StreamReader(FileDB.FileName) If (Not SourceFile Is Nothing) Then Try Console.WriteLine(SourceFile.ReadToEnd()) Catch Except As Exception Console.WriteLine("Error: " & Except.Message) Finally Console.WriteLine("In finally statements") SourceFile.Close() End Try End If Else Console.WriteLine("User selected Cancel") End If End Sub
End Module</source>