【发布时间】:2018-07-13 05:40:49
【问题描述】:
我想使用 Powershell 将文件从 UTF-16 转换为 ANSI,但遇到了一些我不理解的行为。
我的(简化的)脚本是
$encoding = [System.Text.Encoding]::GetEncoding(1250)
$sr = New-Object System.IO.StreamReader -Arg c:\utf16.txt
$sw = New-Object System.IO.StreamWriter c:\new.txt, $false, $encoding
while ($line = $sr.ReadLine()) {
$sw.WriteLine($line)
}
$sr.close()
$sw.Close()
这很好用,输出 new.txt 是 ANSI 编码 但是,如果我改变了
$sw = New-Object System.IO.StreamWriter c:\new.txt, $false, $encoding
到
$sw = New-Object System.IO.StreamWriter c:\new.txt, $encoding
输出 new.txt 采用 UTF-8 编码。 根据StreamWriter Class 文档,这也是定义编码的有效StreamWriter 构造函数。
我错过了什么?
汤姆
【问题讨论】:
-
Get-Content -Path $Path -Encoding Unicode | Set-Content -Path C:\new.txt -Encoding ASCII