In. NET in the System.IO.File and System.IO.FileInfo in the OpenText and appentext several methods are used UTF-8 encoding operation files. This causes garbled behavior if the operation is not a file written with UTF-8 encoding !!!
The workaround is to use a byte throttling operation on the text file, that is, not used. NET provides the OpenText and the Appentext and so on several methods, but is the text as the binary file to operate, and in the operation uses the encoding method to carry on the corresponding decoding. The implementation code is as follows:
To open a text file:
Public Function loadfile (ByVal FileName As String) as String
If filename= "" Then
Return ""
End If
Try
Dim FileReader as FileStream = File.Open (FileName, FileMode.Open)
Dim Filebyte (filereader.length) as Byte
Filereader.read (filebyte, 0, Filereader.length)
' Convert to a system corresponding to the encoded character
Dim Myencoder as Encoding = Encoding.default
Filereader.close ()
FileReader = Nothing
Return New String (Myencoder.getchars (filebyte))
Catch e as Exception
Return ""
End Try
End Sub
Save File:
Public Sub SaveToFile (ByRef conten As String, ByVal FileName as String)
'/* Writes the contents of the Conten string to the filename file
If FileName = "" Then
Exit Sub
End If
Try
Dim filetowrite as FileStream = File.create (FileName)
Dim Rbyte () as Byte = Encoding.Default.GetBytes (Conten.tochararray)
Filetowrite.write (rbyte, 0, Rbyte.length)
Filetowrite.close ()
Filetowrite = Nothing
Catch e as Exception
End Try
End Sub
Using the above two methods, you can implement a text file that uses the encoding of the current system. To avoid garbled problems ....
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.