看流星社区

 找回密码
 注册账号
查看: 2916|回复: 1

[VB] 如何用VB.net 读写Unicode编码的文本

[复制链接]

该用户从未签到

发表于 2014-7-22 08:37:47 | 显示全部楼层 |阅读模式
就是有这么一个文件 里面包括英文和日文 编码是Shift-JIS 我用VB.net要把这个文件写进一个字符串数组(是做词法分析用) 就是把每个单词做为一个String 存到 句子(line,str)这样一个二维数组。

我用的方法:
r = IO.File.OpenRead(strIN)

r.Seek(i, IO.SeekOrigin.Begin)

s = r.ReadByte        

然后让 word = word + Chr(s)

目前读英文没问题。  但是读中文 和 日文的时候,源文件中的日文或者中文部分 完全乱码。

请问怎么解决呢,我也试过ChrW() 还是乱码。

源码如下:
sub AA(ByVal strIN, ByVal strOUT)
  Dim r As System.IO.Stream '读流
        Dim w As System.IO.StreamWriter  '写流
        Dim s As Byte            '字符串
        Dim word As String   '词法,单词      
        Dim fl As Long   '文件长度      
        Dim i As UInteger    '文件指针
        Dim 句(50000, 20) As String  '词法分析用数组,核心
        Dim 行 = 1
        Dim 词 = 1
        Dim x = 1    '暂存变量
        Dim y = 1    '暂存变量

        fl = FileLen(strIN)             '取文件长度
        w = IO.File.AppendText(strOUT)
        r = IO.File.OpenRead(strIN)
        word = ""
        Do Until i = fl

            r.Seek(i, IO.SeekOrigin.Begin)
            s = r.ReadByte                     '顺序读取文件

            If Chr(s) = " " Then    '遇空格则处理word,此时word为一完整单词

                If word <> "" Then
                    句(行, 词) = word   'word写入字符串数组
                    词 = 词 + 1
                    word = ""
                End If

            ElseIf s = 13 Then     '与回车的处理
                句(行, 词) = word
                行 = 行 + 1
                词 = 1
                word = ""
            Else

                word = word + Chr(s)
            End If
            i = i + 1
        Loop
        r.Close()
        MsgBox("文件读入完成")

        Do Until x = 行 + 1                  '将字符串数组中的内容写入一个文件
            Do Until 句(x, y) = ""
                w.Write(句(x, y))

                If 句(x, y + 1) <> "" Then
                    w.Write(" ")
                End If
                y = y + 1
            Loop
            w.Write(Chr(13))
            x = x + 1
            y = 1
        Loop
        w.Close()
        MsgBox("文件写入完成")
    End Sub
非常非常非常非常感谢jyh_jack,

我写了这么一段
dim arr(100) as String
   arr = Split(r.ReadLine, " ")

        Do Until i = 90
            If arr(i) <> "" Then  '不为空则显示字符串,为空则跳过
                MsgBox(arr(i))
            End If
            i = i + 1
        Loop
        r.Close()

但是总是报错 这是咋回事

该用户从未签到

发表于 2014-7-22 08:38:24 | 显示全部楼层
不要用do until,用for:

        Dim arr() As String, i As Integer
        arr = Split(r.ReadLine, " ")
        For i = LBound(arr) To UBound(arr)
            MsgBox(arr(i))
        Next
--------------------
还是用readline,如果你要提取两个非连续空格之间的字符串,可以用
Dim arr() As String
arr = Split(r.ReadLine, " ")
arr()数组里就存了你要的单词。

快要下班了,明后天不上班,如果16:30之前还搞不定就要下个星期1了。

----------------------------------
哈,是我的失误没想到还有可能有半角的日文。
用utf-8就OK了,代码如下:
(文本文件存的时候不能存成ansi,要存成utf-8,或是Unicode)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As System.IO.StreamReader
        r = New System.IO.StreamReader("c:\a.txt", System.Text.Encoding.GetEncoding("utf-8"))
        Do While Not r.EndOfStream
            MsgBox(r.ReadLine)
        Loop
    End Sub
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-24 15:39

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表