在Haxe中使用sys.io.File.read读取文件时指定文件编码 [英] Specifing file encoding while reading a file with sys.io.File.read in Haxe
问题描述
我知道如何使用sys.io.File.read使用Haxe读取文件(比较
I know how to read a file with Haxe by using sys.io.File.read (compare Reading lines from a file in Haxe and I also know that the sys module is not available for each target). However how can I tell sys.io.File.read that my text file is encoded via a certain encoding (e.g. UTF-16, UTF-8, ISO-8859-1, ...)?
推荐答案
无法在File
级别执行此操作,但是您可以在读取文件后对String
进行编码/解码.例如, Utf8.encode()
会将ISO-8859-1字符串转换为一个UTF-8字符串:
There is no way to do this at File
-level, but you can encode / decode the String
after reading the file. For instance, Utf8.encode()
will convert a ISO-8859-1 string to a UTF-8 string:
var isoString = sys.io.File.getContent("iso_file.txt");
var utf8String = haxe.Utf8.encode(isoString);
sys.io.File.saveContent("utf8_file.txt", utf8String);
标准库当前不支持UTF-16,但是它已在Haxe 4中推出.同时,您可以使用诸如 unifill 之类的库.
The standard library currently doesn't support UTF-16, but it's coming in Haxe 4. In the meantime, you can use libraries such as unifill for that.
Btw, if you don't need to read a file line-by-line, File.getContent()
is much more convenient than the File.read()
-approach you linked.
这篇关于在Haxe中使用sys.io.File.read读取文件时指定文件编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!