Streamwriter与Streamreader的默认编码方式是什么

.Net技术 码拜 8年前 (2016-06-03) 1508次浏览
本人查看MSDN,有如下发现
MSDN原话:
StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise
意思是StreamWriter默认使用utf-8喽?
但是在StreamWriter为string类型的构造函数说明
Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size.
使用默认的编码?(在参数为stream时为utf-8)
关于StreamReader MSDN原话:
StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system

那就是说不指定StreamReader编码 就是utf-8?
Streamwriter与Streamreader的默认编码方式是什么
然后本人本人做实验,发生了一个令本人费解的问题,
在调用StreamWirter(string)时 不指定编码 向文本写入汉字,以StreamReader默认读出,是可以正确的解析出汉字。
但是当指定写入的编码方式为Encoding.default,在以streamReader默认读出,居然解析不出汉字啦,出现?号。
大家看看是怎么回事,另外在开发过程中,需要指定读和写的编码方式吗?假如本人的程序可能运行在英文环境的windows中,怎么处理比较好?

解决方案

5

Encoding.default是跟你本地语言环境有关的,像我们中文一般就是gb2312,谁告诉你default是utf-8的

30

StreamWriter和Reader默认使用的是new UTF8Encoding(false, true)

5

引用:

Encoding.default是跟你本地语言环境有关的,像我们中文一般就是gb2312,谁告诉你default是utf-8的

+1


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Streamwriter与Streamreader的默认编码方式是什么
喜欢 (0)
[1034331897@qq.com]
分享 (0)