从文件中读取数据已损坏
我正在从 TXT 文件中读取数据,该文件需要我替换一些现有数据,然后将其写回到文件中。问题是,当我将文本写回文件时,文件中的特殊字符会被损坏。
例如,我在文件“foo.txt”中有一个字符串,其中包含以下“€rdrf +À [HIGH]”。我的应用程序将文本读取到字符串中,遍历该行并将 [HIGH] 替换为值,然后写回文件。问题是,特殊文本字符被损坏。
这是代码库的缩写版本:
string fileText = System.IO.File.ReadAllText("foo.txt");
fileText= iPhoneReferenceText.Replace("[HIGH]", low);
TextWriter tw = new StreamWriter("Path");
tw.WriteLine(fileText);
tw.Close();
如何在不损坏特殊文本字符的情况下读取文件?
谢谢 杰伊
I am reading data from a TXT file that requires me to replace some of the existing data, and then write it back to the file. The issue is, there are special characters in the file that get corrupted when I write the text back to the file.
For example, I have a string in a file "foo.txt" that has the following "€rdrf +À [HIGH]". My application reads the text into a string, goes through the line and replaces [HIGH] with a value, and then writes back to the file. The issue is, the special text characters get corrupted.
Here is an abbreviated version of the code base:
string fileText = System.IO.File.ReadAllText("foo.txt");
fileText= iPhoneReferenceText.Replace("[HIGH]", low);
TextWriter tw = new StreamWriter("Path");
tw.WriteLine(fileText);
tw.Close();
How do I read from the file without corrupting the special text characters?
Thanks
Jay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要适当的编码,
其中 XXXX 是以下之一:
You need an appropriate Encoding I think
Where XXXX is one of:
试试这个:
读者和作者完成后记得丢弃。
Try this :
Remember to Dispose after finishing from the reader and the writer.