Pengetahuan Dasar Membaca & Menulis File dgn Vb net

Namespace yg digunakan : System.IO

Class untuk create, copy,delete,rename direktory : Class Directory

Method  Class Directory  yg sering dipakai:
Exist(path)             :   Mengecheck keberadaan file  dgn return  value boolean
CreateDirectory     :   membuat  directory
Delete(path)          :  menghapus Directory

 

Class untuk  create, copy,delete,rename file : Class File
Method  Class File  yg sering dipakai:
Exist(path)             :   Mengecheck keberadaan file  dgn return  value boolean
Delete(path)          : menghapus file
Copy(source,dest) : copy file dari path sumber ke path tujuan
Move(source,dest) : memindahkan file dari path sumber ke path tujuan

fileio

Class yg digunakan baca tulis file

  1. Class  FileStream, StreamReader dan StreamWriter  : untuk akses file text
  2. Class FileStream, BinaryReader dan BinaryWriter : untuk akses file biner

pada contoh dibawah ini digunakan membaca tulis file text.

Contoh kode VB.Net  akses file

Imports System.IO
Public Class Form1
Private Const dir As String = “C:\direktoriku\”

‘Membuat direktori

Private Sub btnBuatDirektory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuatDirektory.Click
If Not Directory.Exists(dir) Then
Directory.CreateDirectory(dir)
MessageBox.Show(“berhasil”)
Else
MessageBox.Show(“gagal atau direkstory sdh ada”)
End If
End Sub

‘Menghapus direktori

Private Sub btnHapusDirektory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapusDirektory.Click
If Directory.Exists(dir) Then
Directory.Delete(dir)
MessageBox.Show(“berhasilhapus Dir”)
Else
MessageBox.Show(“hapus gagal atau DIR tdk ada”)
End If
End Sub

‘Membuat file fileku.txt di dlm direktori C:\direktoriku\

Private Sub btnBuatFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuatFile.Click
If Not File.Exists(dir + “fileku.txt“) Then
File.Create(dir + “fileku.txt“)
MessageBox.Show(“berhasil”)

FileClose()
End If
End Sub

‘Menghapus file fileku.txt

Private Sub btnHapusFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapusFile.Click
If File.Exists(dir + “fileku.txt“) Then
File.Delete(dir + “fileku.txt“)
MessageBox.Show(“berhasilhapus”)
FileClose()
End If
End Sub

‘Menulis  data string ke file

Private Sub btnWriteFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteFile.Click
Dim konekFile As New StreamWriter(dir + “fileku.txt“)

konekFile.Write(TextBox1.Text + “, “)
konekFile.Write(TextBox2.Text + “, “)
konekFile.WriteLine(TextBox3.Text)

konekFile.Close()

‘Membaca semua isi file

End Sub

Private Sub btnReadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim konekFile As New StreamReader(dir + “fileku.txt“)
txtAll.Text = konekFile.ReadToEnd
konekFile.Close()

End Sub

‘Membaca data file perbaris

Private Sub btnReadBaris_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadBaris.Click
Dim objBacaFile1 As New StreamReader(dir + “fileku.txt“)
TextBox1.Text = objBacaFile1.ReadLine
TextBox2.Text = objBacaFile1.ReadLine
TextBox3.Text = objBacaFile1.ReadLine
objBacaFile1.Close()
End Sub

‘Menulis data tanpa menghapus data yg ada  dlm file

Private Sub btnWriteAppend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteAppend.Click
Dim objTulisFile As New StreamWriter(New FileStream(dir + “fileku.txt“, FileMode.Append, FileAccess.Write))
objTulisFile.Write(TextBox1.Text + “,”)
objTulisFile.Write(TextBox2.Text + “,”)
objTulisFile.WriteLine(TextBox3.Text)
objTulisFile.Close()
End Sub

‘Membaca data file per baris

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objBacaFile As New StreamReader(New FileStream(dir + “fileku.txt“, FileMode.OpenOrCreate, FileAccess.ReadWrite))
TextBox1.Text = objBacaFile.ReadLine
TextBox2.Text = objBacaFile.ReadLine
TextBox3.Text = objBacaFile.ReadLine
objBacaFile.Close()
End Sub

‘ Membaca  file dan parsing   data string  , contoh isi file: 123,765,987,..dst

Private Sub btnReadSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSplit.Click
Dim objBacaFile3 As New StreamReader(New FileStream(dir + “fileku.txt“, FileMode.OpenOrCreate, FileAccess.ReadWrite))

Do While objBacaFile3.Peek <> -1

Dim baris As String = objBacaFile3.ReadLine
Dim dataKolom() As String = baris.Split(CChar(“,”))
TextBox1.Text = dataKolom(0)
TextBox2.Text = dataKolom(1)
TextBox3.Text = dataKolom(2)

Loop

objBacaFile3.Close()

End Sub
End Class

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 26/08/2016, in Daftar isi, Visual Basic . Net. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: