本文介绍了如何将文本文件的编码格式从Unicode更改为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim txtFile, fileObj, streamObj, s

Set txtFile = CreateObject(fileName)

Set streamObj = CreatreObject("adodb.Stream")
streamObj.Charset = "UTF-8"
streamObj.open
Set fileObj = txtFile.OpenTextFile("filePath")

Do Until fileObj.AtEndOfStream
      s = fileObj.ReadLine
      txtObj.WriteText s
Loop

txtObj.SaveToFile "D:\A4\Message_tool\surya.msg", 2
fileObj.Close

执行此代码后,surya.msg的编码格式为"ANSCII",但我希望它为"UTF-8"

After the execution this code the encoding format of surya.msg is "ANSCII" but I want it to be "UTF-8"

推荐答案

Const adTypeText = 2
Const adSaveCreateOverWrite = 2

Dim inputFile, outputFile
    inputFile = "input_file.txt"
    outputFile = "output_file.txt"

Dim inputStream
    Set inputStream  = WScript.CreateObject("adodb.stream")
    With inputStream
        .Type = adTypeText
        .Charset = "unicode"
        .Open
        .LoadFromFile inputFile
    End With

Dim outputStream
    Set outputStream = WScript.CreateObject("adodb.stream")
    With outputStream
        .Type = adTypeText
        .Charset = "utf-8"
        .Open
        .WriteText inputStream.ReadText
        .SaveToFile outputFile, adSaveCreateOverWrite
    End With

    inputStream.Close
    outputStream.Close

这篇关于如何将文本文件的编码格式从Unicode更改为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:51