VB.Net Tutorial/Stream File/StreamReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Содержание
Check Exception in file reading
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
Hello world!
Create StreamReader from FileStream
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
Read all text in a text file by using StreamReader
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
<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
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
Read text file to the end
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
StreamReader: Peek
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
StreamReader: read to a buffer
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
Use Finally clause to close a stream
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