从文件中读取数据已损坏

发布于 2024-09-27 09:00:24 字数 472 浏览 4 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-10-04 09:00:24

我认为您需要适当的编码,

string fileText = System.IO.File.ReadAllText("foo.txt", Encoding.XXXX);
.
.
tw = new StreamWriter("path", Encoding.XXXX);
.
.

其中 XXXX 是以下之一:

  System.Text.ASCIIEncoding
  System.Text.UnicodeEncoding
  System.Text.UTF7Encoding
  System.Text.UTF8Encoding

You need an appropriate Encoding I think

string fileText = System.IO.File.ReadAllText("foo.txt", Encoding.XXXX);
.
.
tw = new StreamWriter("path", Encoding.XXXX);
.
.

Where XXXX is one of:

  System.Text.ASCIIEncoding
  System.Text.UnicodeEncoding
  System.Text.UTF7Encoding
  System.Text.UTF8Encoding
三五鸿雁 2024-10-04 09:00:24

试试这个:

        string filePath = "your file path";
        StreamReader reader = new StreamReader(filePath);
        string text = reader.ReadToEnd();
        // now you edit your text as you want
        string updatedText = text.Replace("[HIGH]", "[LOW]");

        reader.Dispose(); //remember to dispose the reader so you can overwrite on the same file
        StreamWriter writer = new StreamWriter(filePath);
        writer.Write(text, 0, text.Length);
        writer.Dispose(); //dispose the writer
        Console.ReadLine();

读者和作者完成后记得丢弃。

Try this :

        string filePath = "your file path";
        StreamReader reader = new StreamReader(filePath);
        string text = reader.ReadToEnd();
        // now you edit your text as you want
        string updatedText = text.Replace("[HIGH]", "[LOW]");

        reader.Dispose(); //remember to dispose the reader so you can overwrite on the same file
        StreamWriter writer = new StreamWriter(filePath);
        writer.Write(text, 0, text.Length);
        writer.Dispose(); //dispose the writer
        Console.ReadLine();

Remember to Dispose after finishing from the reader and the writer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文