c#对txt文件的写入和读取

v7_123 2018-03-08 10:20:27

为什么我读取txt文件的时候,中文会是乱码?
也有时候写入的中文,在存入txt文件的时候也是乱码。
应该怎么样解决,求详细的代码,谢谢!
...全文
786 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
zj5070711236 2018-03-09
  • 打赏
  • 举报
回复
txt文件格式与数据流格式都需要保持一致:UTF-8
hhhhha123 2018-03-09
  • 打赏
  • 举报
回复
utf-8 .
WHNIS 2018-03-09
  • 打赏
  • 举报
回复
创建一个新的类,添加以下方法,直接调用即可,自动创建文件与文件夹 public void WriteStr(string str) { string DirectoryPath = Directory.GetCurrentDirectory() + "\\LOG\\"; string name = “Student”; string filePath = DirectoryPath + name + ".txt"; /* 判断写入的文件夹是否存在,不存在创建一个 */ if (!Directory.Exists(DirectoryPath)) { Directory.CreateDirectory(DirectoryPath); } /* 判断写入的文件是否存在 */ /* 不存在创建并写入数据 */ if (!File.Exists(filePath)) { FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); StreamWriter sw1 = new StreamWriter(fs1); sw1.Write(str + "\r\n"); sw1.Close(); fs1.Close(); } /* 存在则追加数据 */ else { FileStream fs2 = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); StreamWriter sw2 = new StreamWriter(fs2); sw2.Write(str + "\r\n"); sw2.Close(); fs2.Close(); } }
xiaoxiangqing 2018-03-09
  • 打赏
  • 举报
回复
读文件可以指定编码格式
stevenjin 2018-03-09
  • 打赏
  • 举报
回复
用代码创建试试
Roselle_2013 2018-03-09
  • 打赏
  • 举报
回复
我记得我之前哪次好像也有这样过,解决方法很简单,不手动创建TXT文件,而是用代码创建文件。File.Create。
xg436 2018-03-09
  • 打赏
  • 举报
回复
System.IO.File.ReadAllLines("D:test.txt", System.Text.Encoding.GetEncoding("gb2312"))[0];
Dogfish 2018-03-09
  • 打赏
  • 举报
回复
你的文件是不是utf8编码?
ybbwfnvsgps 2018-03-09
  • 打赏
  • 举报
回复
格式错误,改成utf-8
v7_123 2018-03-08
  • 打赏
  • 举报
回复
引用 8楼xuzuning 的回复:
流(Stream)方式的文件操作,默认就是 utf-8 编码的,只在所需编码不是 utf-8 时才需指定编码 你指定了 utf-8,但却出现了乱码,显然你需要的不是 utf-8 编码
那汉字写入txt文件用那个语句
吉普赛的歌 2018-03-08
  • 打赏
  • 举报
回复
,Encoding.Default 试试
v7_123 2018-03-08
  • 打赏
  • 举报
回复
引用 8楼xuzuning 的回复:
流(Stream)方式的文件操作,默认就是 utf-8 编码的,只在所需编码不是 utf-8 时才需指定编码 你指定了 utf-8,但却出现了乱码,显然你需要的不是 utf-8 编码
大佬可以稍微改一下我的代码吗?
xuzuning 2018-03-08
  • 打赏
  • 举报
回复
流(Stream)方式的文件操作,默认就是 utf-8 编码的,只在所需编码不是 utf-8 时才需指定编码 你指定了 utf-8,但却出现了乱码,显然你需要的不是 utf-8 编码
  • 打赏
  • 举报
回复
都指定编码格式是Utf-8,不要有的地方指定,有的地方不指定
v7_123 2018-03-08
  • 打赏
  • 举报
回复
引用 4楼正怒月神 的回复:
设置编码格式 utf-8.
string pathStuLogin = @"C:\Users\vicii\Desktop\课程设计\StudentLogin.txt";
StreamWriter sw1 = new StreamWriter(pathStuLogin, true,Encoding.UTF8);
string mystrName = textBox1.Text.ToString();
string mystrPassword = mystrName.Remove(0, 8);//读取学号后四位,作为密码
sw1.WriteLine("用户名" + mystrName + "密码" + mystrPassword);
sw1.Close(); 为什么我的这个写入txt,写入的汉字是乱码,怎么解决?
正怒月神 2018-03-08
  • 打赏
  • 举报
回复
设置编码格式 utf-8.
v7_123 2018-03-08
  • 打赏
  • 举报
回复
引用 2楼StratosBlue 的回复:
看一眼很眼熟。。。然后。。。 我有个问题,我之前写的代码有什么问题吗?
什么鬼哦……
Anonymous477 2018-03-08
  • 打赏
  • 举报
回复
引用 3 楼 yuqiugong1002 的回复:
[quote=引用 2楼StratosBlue 的回复:] 看一眼很眼熟。。。然后。。。 我有个问题,我之前写的代码有什么问题吗?
什么鬼哦……[/quote] 哦?抱歉,打扰,告辞
Anonymous477 2018-03-08
  • 打赏
  • 举报
回复
看一眼很眼熟。。。然后。。。 我有个问题,我之前写的代码有什么问题吗?
秋的红果实 2018-03-08
  • 打赏
  • 举报
回复
查看下你txt文件的编码格式,打开txt,另存为,就可以看到编码方式,应该使用utf-8
加载更多回复(4)

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧