读中文文本的问题

mingyicz 2008-04-24 08:26:13
Imports System
Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As StreamReader = New StreamReader("d:\myfile.txt")
Dim sw As StreamWriter = New StreamWriter("d:\outfile.txt")
Dim mystr1, mystr2, mystr3, line As String
Do
line = sr.ReadLine
If line Is Nothing Then
Exit Do
End If
If (Not String.IsNullOrEmpty(line)) AndAlso line.Length > 4 Then
mystr1 = line.Substring(0, 2)
mystr2 = line.Substring(2, 12)
mystr3 = line.Substring(14, 8)
sw.WriteLine(mystr1 + "," + mystr2 + "," + mystr3)
End If
Loop Until line Is Nothing
sr.Close()
sw.Close()
End Sub
End Class
’----------------------------------------------
myfile.txt的格式:
th 伊科拉木INF 20080306

结果却是:
th, ������ľINF, 20080
本来是mystr1读取:th,mystr2读取中间部分,mystr3读取:20080306的,结果怎么会是这个样子呢?还是乱码......-.-
...全文
88 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghongbin8494 2008-04-25
  • 打赏
  • 举报
回复
Dim mytxt As String = My.Computer.FileSystem.ReadAllText("c:\1.txt", System.Text.Encoding.Default)

测试通过
路人乙e 2008-04-25
  • 打赏
  • 举报
回复
string[] strs = line.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
mystr1 = strs[0].Trim();
mystr2 = strs[1].Trim();
mystr3 = strs[2].Trim();
llsus 2008-04-25
  • 打赏
  • 举报
回复
可能你的文件格式不是ANSI编码,所有乱嘛,你必须用文件本来编码来读去才行

你读取后不用SubString, 应该用
     Dim S As String
S = line.Replace(" ", ",")
mingyicz 2008-04-25
  • 打赏
  • 举报
回复
6楼的解决方法可以解决中文的问题
可是我想要的结果是:
th, 伊科拉木INF ,20080306
而只改encoding的话结果是这样的:
th, 伊科拉木INF, 20080




zwnylsf 2008-04-24
  • 打赏
  • 举报
回复

Dim sr As StreamReader = New StreamReader("d:\myfile.txt") 改为
Dim sr As StreamReader = New StreamReader(("d:\myfile.txt", System.Text.Encoding.Default, False)
这主要是由于你没有设置System.Text.Encoding,设置为Default,系统就会自动为导入的文本文件使用合适的字符解码
tjficcbw 2008-04-24
  • 打赏
  • 举报
回复

Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click



Dim sr As TextReader = New StreamReader("d:\myfile.txt")
Dim sw As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("d:\outfile.txt", True)
Dim mystr1 = "", mystr2 = "", mystr3 = "", line As String
Do
line = sr.ReadLine
'MsgBox(line)
If line Is Nothing Then
Exit Do
End If
mystr1 = line.Substring(0, 2)
mystr2 = line.Substring(2, 12)
mystr3 = line.Substring(14, 8)
'sw.WriteLine(line)
sw.WriteLine(mystr1 + "," + mystr2 + "," + mystr3)
Loop Until line Is Nothing
sr.Close()
sw.Close()
End Sub


tjficcbw 2008-04-24
  • 打赏
  • 举报
回复
Dim sr As TextReader = New StreamReader("d:\myfile.txt")
line = sr.ReadLine
MsgBox(line)
mingyicz 2008-04-24
  • 打赏
  • 举报
回复
未声明Encoding 的错误-。-
能否解释下UTF。。。。。。。。。。。。。。。
vlsm 2008-04-24
  • 打赏
  • 举报
回复
//打开文件
StreamReader reader = new StreamReader("test.txt", System.Text.Encoding.GetEncoding("gb2312"));
//或用这个编码格式
//StreamReader reader = new StreamReader("test.txt",System.Text.Encoding.Default);
//读取流
this.textBox1.Text = reader.ReadToEnd();
//关闭流
reader.Close();

//再或者 直接用这个方式
//richTextBox1.LoadFile("D:\\5.txt", RichTextBoxStreamType.RichText);
jzywh 2008-04-24
  • 打赏
  • 举报
回复
加Encoding


Dim sr As StreamReader = New StreamReader("d:\myfile.txt", Encoding.UTF8)



Dim sw As StreamWriter = New StreamWriter("d:\outfile.txt", Encoding.UTF8)

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧