Imports system.io
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fn As FileStream
Try
fn = New FileStream("c:\x1.txt", FileMode.OpenOrCreate, FileAccess.Write)
Catch ex As Exception
MsgBox("can not open")
End Try
Dim s(2) As Byte
s(0) = Asc("ก")
s(1) = Asc("ฮ")
fn.Write(s, 0, s.Length)
fn.Close() ' file size = 3 bytes
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sw As StreamWriter = New StreamWriter("c:\x2.txt")
Dim s As String = "กขค"
sw.Write(s)
sw.Close()
sw = New StreamWriter("c:\x3.txt") '("",false,Encoding.GetEncoding("windows-874"))
Dim i As Integer = 12
sw.Write(i)
sw.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim fn As FileStream
Try
fn = New FileStream("c:\x1.txt", FileMode.Open)
Catch ex As Exception
MsgBox("can not open")
End Try
Dim data As Integer
Dim c() As Char
c &= Chr(Asc("ก"))
Do
data = fn.ReadByte()
If (data <> -1) Then c &= Chr(data)
Loop While (data <> -1)
MessageBox.Show(c)
fn.Close() ' file size = 3 bytes
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
Dim path As String = "c:\x1.txt"
If Not File.Exists(path) Then Exit Sub
Dim fn As FileStream = New FileStream(path, FileMode.Open)
Dim sr As StreamReader = New StreamReader(fn, Encoding.Default)
Do While sr.Peek() >= 0
MsgBox(sr.ReadLine()) ' show each line
Loop
fn.Seek(0, SeekOrigin.Begin)
MsgBox(sr.ReadToEnd()) ' show all in msgbox
fn.Close()
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
End Class
|