首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileSystemObject -读取Unicode文件

FileSystemObject -读取Unicode文件
EN

Stack Overflow用户
提问于 2009-09-11 11:21:13
回答 5查看 28.2K关注 0票数 12

经典的ASP,VBScript上下文。

包括这个微软的在内的许多文章都表示,不能使用FileSystemObject读取Unicode文件。

我在前一段时间遇到了这个问题,因此根据ReadText示例这里,我转而使用FileSystemObject.OpenTextFile (它确实接受一个指示是否以unicode的形式打开文件的最终参数,但实际上无法工作)。

但是,ADODB.Stream在尝试读取UNC文件共享(与权限相关的问题)上的文件时,会造成极大的痛苦。因此,对此进行调查时,我无意中发现了以下方法:( a)使用unicode文件;( b)跨UNC文件程序:

代码语言:javascript
复制
dim fso, file, stream
set fso = Server.CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile("\\SomeServer\Somefile.txt")
set stream = file.OpenAsTextStream(ForReading,-1) '-1 = unicode

这是使用FSO来读取unicode文件,没有任何明显的问题,所以我对所有的引用感到困惑,包括MS,因为您不能使用FSO来读取unicode文件。

有其他人使用这种方法读取unicode文件吗?我遗漏了什么隐藏的问题吗?或者您真的可以使用FSO读取unicode文件吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-09-11 13:02:57

是的,文件已经过时了。脚本组件在其早期确实经历了一系列更改(如果您使用早期绑定,其中一些更改会中断),但是由于至少WK2000、SP4和XP SP2,它非常稳定。

只是小心你所说的unicode。有时,unicode这个词被更广泛地使用,可以涵盖unicode的任何编码。例如,FSO不读取unicode的UTF8编码。为此,您需要回到ADODB.Stream上。

票数 4
EN

Stack Overflow用户

发布于 2009-09-23 11:21:18

我认为MS没有正式声明它支持unicode,因为:

  1. 它不使用文件开头的字节顺序标记检测unicode文件,并且
  2. 它只支持16 unicode文件(如果存在,您需要删除字节顺序标记)。

下面是一些示例代码,我已经成功地使用这些代码(几年来)来使用FSO自动检测和读取unicode文件(假设它们是小endian并包含BOM):

代码语言:javascript
复制
'Detect Unicode Files
Set Stream = FSO.OpenTextFile(ScriptFolderObject.Path & "\" & FileName, 1, False)
intAsc1Chr = Asc(Stream.Read(1))
intAsc2Chr = Asc(Stream.Read(1))
Stream.Close
If intAsc1Chr = 255 And intAsc2Chr = 254 Then 
    OpenAsUnicode = True
Else
    OpenAsUnicode = False
End If

'Get script content
Set Stream = FSO.OpenTextFile(ScriptFolderObject.Path & "\" & FileName, 1, 0, OpenAsUnicode)
TextContent = Stream.ReadAll()
Stream.Close
票数 11
EN

Stack Overflow用户

发布于 2012-11-08 14:18:54

代码语言:javascript
复制
'assume we have detected that it is Unicode file - then very straightforward 
'byte-by-byte crawling sorted out my problem:
'.
'.
'.
else
   eilute=f.ReadAll
   'response.write("&#268;IA BUVO &#268;ARLIS<br/>")
   'response.write(len(eilute))
   'response.write("<br/>")
   elt=""
   smbl=""
   for i=3 to len(eilute)  'First 2 bytes are 255 and 254
     baitas=asc(mid(eilute,i,1)) 
     if (i+1) <= len(eilute) then
      i=i+1 
    else
     exit for
    end if
    antras=asc(mid(eilute,i,1))*256 ' raidems uzteks
    'response.write(baitas)
    'response.write(asc(mid(eilute,i,1)))
    'response.write("<br/>")
    if baitas=13 and antras=0 then 'LineFeed
      response.write(elt)
      response.write("<br/>")
      elt=""
      if (i+2) <= len(eilute) then i=i+2 'persokam per CarriageReturn
    else
      skaicius=antras+baitas
      smbl="&#" & skaicius & ";"
      elt=elt & smbl
    end if
    next
   if elt<>"" then
    response.write(elt)
    response.write("<br/>")
    elt=""
   end if
  end if
 f.Close
 '.
 '.
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1410334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档