Untuk membaca dan menulis file teks
(*.txt) dengan VB.NET dapat menggunakan beberapa cara, salah satunya
adalah dengan menggunakan kelas StreamReader dan kelas StreamWriter yang terdapat pada System.IO package.
Berikut ini adalah contoh function untuk membaca seluruh isi file teks dengan menggunakan method ReadToEnd pada class StreamReader:
01Public Function BacaSeluruhFile(ByVal FullPath As String)
02 Dim fileReader As StreamReader
03 Dim content As String = ""
04 Try
05 fileReader = New StreamReader(FullPath)
06 content = fileReader.ReadToEnd()
07 fileReader.Close()
08 Catch x As Exception
09 MsgBox(x.Message)
10 End Try
11 Return content
12End Function
Sedangkan yang berikut ini adalah contoh function untuk membaca isi file teks pada baris tertentu dengan menggunakan method ReadLine pada class StreamReader:
01Public Function BacaBarisKe(ByVal FullPath As String, _
02ByVal baris As Integer)
03 Dim fileReader As StreamReader
04 Dim content As String = ""
05 Dim i As Integer
06 Try
07 fileReader = New StreamReader(FullPath)
08 For i = 1 To baris
09 content = fileReader.ReadLine()
10 Next
11 fileReader.Close()
12 Catch x As Exception
13 MsgBox(x.Message)
14 End Try
15 Return content
16End Function
Adapun untuk menulis String ke dalam sebuah file teks dapat mengunakan method Write pada class StreamWriter seperti pada procedure dalam contoh berikut ini:
01Public Sub WriteContent(ByVal FullPath As String, _
02ByVal content As String)
03 Dim fileWriter As StreamWriter
04 Try
05 fileWriter = New StreamWriter(FullPath)
06 fileWriter.Write(content)
07 fileWriter.Close()
08 Catch x As Exception
09 MsgBox(x.Message)
10 End Try
11End Sub
Yang penting, jangan lupa mengimport System.IO package dengan menuliskan:
1Imports System.IO
pada bagian global declaration. Semoga bermanfaat
01 | Public Function BacaSeluruhFile(ByVal FullPath As String) |
02 | Dim fileReader As StreamReader |
03 | Dim content As String = "" |
04 | Try |
05 | fileReader = New StreamReader(FullPath) |
06 | content = fileReader.ReadToEnd() |
07 | fileReader.Close() |
08 | Catch x As Exception |
09 | MsgBox(x.Message) |
10 | End Try |
11 | Return content |
12 | End Function |
01 | Public Function BacaBarisKe(ByVal FullPath As String, _ |
02 | ByVal baris As Integer) |
03 | Dim fileReader As StreamReader |
04 | Dim content As String = "" |
05 | Dim i As Integer |
06 | Try |
07 | fileReader = New StreamReader(FullPath) |
08 | For i = 1 To baris |
09 | content = fileReader.ReadLine() |
10 | Next |
11 | fileReader.Close() |
12 | Catch x As Exception |
13 | MsgBox(x.Message) |
14 | End Try |
15 | Return content |
16 | End Function |
01 | Public Sub WriteContent(ByVal FullPath As String, _ |
02 | ByVal content As String) |
03 | Dim fileWriter As StreamWriter |
04 | Try |
05 | fileWriter = New StreamWriter(FullPath) |
06 | fileWriter.Write(content) |
07 | fileWriter.Close() |
08 | Catch x As Exception |
09 | MsgBox(x.Message) |
10 | End Try |
11 | End Sub |
1 | Imports System.IO |






0 komentar:
Post a Comment