Private Sub Command1_Click()
Writein “C:\Users\Administrator\Desktop\1.txt”, “文本文内容”
End Sub
Private Sub Form_Load()
Text1 = ReadANSI(“C:\Users\Administrator\Desktop\1.txt”)
Text2 = ReadUTF8(“C:\Users\Administrator\Desktop\1.txt”)
End Sub
'写入TXT文件
Private Function Writein(ByVal txtpath As String, ByVal txt As String)
On Error Resume Next
Open txtpath For Output As #1 '打开XXX路径的XXX文件(双引号里表示文件位置和文件名)
Print #1, txt '写入Text1的Text内容
Close #1 '关闭
End Function
'防止 记事本 乱码
Private Function ReadUTF8(Path As String)
Dim EncodingMode As String '编码模式
Dim arrBinary() As Byte
EncodingMode = “UTF-8”
Open Path For Binary As #1
ReDim arrBinary(LOF(1) - 1)
Get #1, , arrBinary()
Close #1
ReadUTF8 = BytesToBstr(arrBinary, EncodingMode)
End Function
Private Function ReadANSI(Path As String) '普通模式 GB2312
Dim EncodingMode As String '编码模式
Dim arrBinary() As Byte
EncodingMode = “GB2312”
Open Path For Binary As #1
ReDim arrBinary(LOF(1) - 1)
Get #1, , arrBinary()
Close #1
ReadANSI = BytesToBstr(arrBinary, EncodingMode)
End Function
Private Function BytesToBstr(Binary, Unicode)
Dim objstream As Object
Set objstream = CreateObject(“ADODB.Stream”)
objstream.Type = 1
objstream.Mode = 3
objstream.Open
objstream.Write Binary
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Unicode
BytesToBstr = objstream.ReadText
objstream.Close
End Function