sherwood5 发表于 2014-7-22 08:37:47

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

就是有这么一个文件 里面包括英文和日文 编码是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()

但是总是报错 这是咋回事

gaofeng 发表于 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
页: [1]
查看完整版本: 如何用VB.net 读写Unicode编码的文本