关于读取txt文件中文乱码问题
在处理文件的过程中,读取txt文件出现中文乱码。这种情况是由于编码字符不一致导致。
public static string ReadFile(string path, string fileName)
{
FileStream stream = null;
StreamReader reader = null;
StringBuilder v = new StringBuilder(); try
{
stream = new FileStream(path + fileName, FileMode.Open);
reader = new StreamReader(stream,Encoding.GetEncoding("GB2312")); string line = reader.ReadLine();
do
{
v.Append(line);
line = reader.ReadLine();
}
while (!string.IsNullOrEmpty(line));
}
catch
{
}
finally
{
if (stream != null)
{
stream.Close();
stream.Dispose();
}
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
return v.ToString();
}
转换后可得正确结果
关于读取txt文件中文乱码问题的更多相关文章
- c# 读取txt文件中文乱码解决方法
之前做过一个项目,在程序运行目录下有个txt文件,文件内容是中文的时候会乱码, 后来用这个函数处理后,就不乱码了: private string GetPDA_Code() { ...
- 解決BufferedReader读取UTF-8文件中文乱码
解決BufferedReader读取UTF-8文件中文乱码 File rst01 = new File(context.getRealPath("/")+" ...
- 【其他】【navicat】【1】navicat导入txt文件中文乱码问题解决
正文: TXT文件默认编码为ANSI,另存为编码为UTF-8的文本文件即可 备注: 1,一般需要导入的数据都是一张excel表,需要将excel表另存为“文本文件(制表符分隔)(*.txt)”保存类型 ...
- iOS-读取txt文件中文乱码
一.情景描述: 后台给一个txt文件,编码是utf-8,在Mac电脑Xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况. 二.尝试解决方法: 修改编码格式,尝试了NSUTF1 ...
- 解決BufferedReader读取UTF-8文件中文乱码(转)
读取txt文件乱码 BufferedReader read = new BufferedReader(new FileReader(new File(filename))); 解决办法: InputS ...
- Python读取 csv文件中文乱码处理
需求:按行解析读取csv文件存入关系型数据库——主要是中文字体解析:遇到的问题:直接解析出来的数据为list形式,而且编码格式为unicode;解决问题:前提了解: 中文编码的规则 —— GB2312 ...
- [转载] linux下打开windows txt文件中文乱码问题
原文链接 在linux操作系统下,我们有时打开在windows下的txt文件,发现在windows下能正常显示的txt文件出现了中文乱码. 出现这种情况的原因为两种操作系统的中文压缩方式不同,在win ...
- arcgis for android 读取shp文件中文乱码解决方法
设置注册表默认字符,即可解决中文乱码问题. 'dbfDefault' 设置方法1.开始--运行,输入”Regedit“,打开注册表.2.如是用的是 10.x 版本 ArcGIS Desktop,定位到 ...
- Atom打开txt文件中文乱码解决、指定文件的语法格式、win10中禁止睡眠
1.Atom中文乱码解决 首先保证打开的txt文件的编码格式为UTF-8无BOM编码格式,可以使用Notepad++更改,如下图所示: 然后再在atom中打开文件,并右键点击文件内容的任意位置,Cha ...
随机推荐
- cx_Oracle 报错 Reason: image not found
(Study_env) ➜ DAL python -c "import cx_Oracle"Traceback (most recent call last): File &quo ...
- HTML中图片添加
图片添加后保存的是添加路径 例: <div class="form-group"> <label class="col-sm-3 control-lab ...
- [转]在WPF中区别TextBlock和Label
TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...
- css书写规范及特殊样式
1.CSS书写顺序: (1)位置:position.top.right.z-index.display.float (2)大小:width.height.padding.margin (3)文字系列: ...
- All About Python
Part one: Learn the Basics Hello, World! print "Hello,World!" Variables and Types Python i ...
- EF 底层基础方法
1 using System; 2 using System.Data; 3 using System.Collections.Generic; 4 using System.Data.Entity; ...
- XproerIM V2开源,基于XproerUI引擎构架
资源下载:XproerIM源代码(访问密码:2e47),imServer(访问密码 364c), 技术交流群:6259765 技术支持:3040217208 咨询邮箱:2190615607@qq.co ...
- mysql优化要点(一)
一 sql语句中使用函数需要注意,因为有可能结果每条语句都会调用,甚至表内每条记录都会调用. 二 注意转换,会消耗大量性能,甚至字符串的encoding不同,也会出现非常大的消耗 三 myisa ...
- 在 .NET 4.0 中使用 .NET 4.5 中新增的特性(CallerMemberNameAttribute/CallerFilePathAttribute/CallerLineNumberAttribute)
介绍 标题中所说的三个特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 我们统称为调 ...
- php底层
http://www.phpchina.com/member.php?mod=logging&action=login 我们从未手动开启过PHP的相关进程,它是随着Apache的启动而运行的: ...