使用 StreamWriter 在文件中写入 Unicode 字符串不起作用

发布于 2024-12-17 06:45:41 字数 221 浏览 0 评论 0原文

我有这段代码:

string s = "آ";
StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8);
writer.WriteLine(s);

但是当我运行它时,我在 a.txt 中看不到任何“Ë”! a.txt 中没有任何字符串!它是空的!有什么问题!!谁能帮我吗???

I have this code:

string s = "آ";
StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8);
writer.WriteLine(s);

but when I run it I can't see any "آ" in a.txt!! There isn't any string in a.txt! It is Empty! What is problem!?! Can anyone help me???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

眸中客 2024-12-24 06:45:41

您永远不会Close() StreamWriter

如果您在完成写入后调用 writer.Close(),您将看到该字符。

但是,由于它实现了 IDisposable,您应该将 StreamWriter 的创建包装在 using 语句中:

using(StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8))
{
   writer.WriteLine(s);
}

这将为您关闭流。

You never Close() the StreamWriter.

If you call writer.Close() when you finish writing, you will see the character.

But, since it implements IDisposable you should wrap the creation of the StreamWriter in a using statement:

using(StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8))
{
   writer.WriteLine(s);
}

This will close the stream for you.

通知家属抬走 2024-12-24 06:45:41

从外观上看,您不是 Flush ()ing 或 Close()StreamWriterStreamWriter 在内部使用一个缓冲区,在关闭应用程序之前需要刷新该缓冲区,否则 StreamWriter 超出范围,否则您写入的数据将不会写入光盘。

完成后您可以调用 Close() - 尽管我建议使用 using 语句也可确保您的 StreamWriter 得到正确处置。

string s = "آ";

using (StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8))
{
    writer.WriteLine(s);
}

By the looks of it you're not Flush()ing or Close()ing the StreamWriter before you end your application. StreamWriter uses a buffer internally which needs to be flushed before you close your application or the StreamWriter goes out of scope otherwise the data you wrote to it will not be written to disc.

You can call Close() once you're done - although instead I would suggest using a using statement instead to also assure that your StreamWriter gets properly disposed.

string s = "آ";

using (StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8))
{
    writer.WriteLine(s);
}
〆凄凉。 2024-12-24 06:45:41

尝试使用 File.WriteAllText("a.txt", s, Encoding.UTF8);。

Try using File.WriteAllText("a.txt", s, Encoding.UTF8);.

白日梦 2024-12-24 06:45:41

一些提示:

  • 您是否在文件中看到任何字符代替了您期望的字符?如果不是,您不会刷新并关闭流
  • StreamWriter 应该能够编写 unicode,而无需选择编码,但您可以尝试使用 UTF32 编码。

查看如何:将文本写入文件

Few tips:

  • do you see any character in the file written in place of one you expect? If not you're not flushing and closing the stream
  • StreamWriter should be able to write unicode without you having to choose encoding, but you could try to use UTF32 encoding.

Check How to: Write Text to a File

感情废物 2024-12-24 06:45:41

接下来,使用正确的特殊字符将列表视图导出到 Excel 的完整功能:

private void Exportar()
    {
        Encoding encoding = Encoding.UTF8;

        saveFileDialog1.Filter = "Arquivo Excel (*.xls)|*.xls";
        saveFileDialog1.FileName = "logs";
        saveFileDialog1.Title = "Exportar para Excel";
        StringBuilder sb = new StringBuilder();
        foreach (ColumnHeader ch in lstPesquisa.Columns)
        {
            sb.Append(ch.Text + "\t");
        }
        sb.AppendLine();
        foreach (ListViewItem lvi in lstPesquisa.Items)
        {
            foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
            {
                if (lvs.Text.Trim() == string.Empty)
                {
                    sb.Append(" ");
                }
                else
                {
                    string ITEM = Regex.Replace(lvs.Text, @"\t|\n|\r", "");//remover novas linhas
                    sb.Append(ITEM + "\t");
                }
            }
            sb.AppendLine();
        }
        DialogResult dr = saveFileDialog1.ShowDialog();
        if (dr == DialogResult.OK)
        {
            StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF32);
            sw.Write(sb.ToString());
            sw.Close();
        }
    }

Follow, complete function for export a listview to excel with correct special characteres:

private void Exportar()
    {
        Encoding encoding = Encoding.UTF8;

        saveFileDialog1.Filter = "Arquivo Excel (*.xls)|*.xls";
        saveFileDialog1.FileName = "logs";
        saveFileDialog1.Title = "Exportar para Excel";
        StringBuilder sb = new StringBuilder();
        foreach (ColumnHeader ch in lstPesquisa.Columns)
        {
            sb.Append(ch.Text + "\t");
        }
        sb.AppendLine();
        foreach (ListViewItem lvi in lstPesquisa.Items)
        {
            foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
            {
                if (lvs.Text.Trim() == string.Empty)
                {
                    sb.Append(" ");
                }
                else
                {
                    string ITEM = Regex.Replace(lvs.Text, @"\t|\n|\r", "");//remover novas linhas
                    sb.Append(ITEM + "\t");
                }
            }
            sb.AppendLine();
        }
        DialogResult dr = saveFileDialog1.ShowDialog();
        if (dr == DialogResult.OK)
        {
            StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF32);
            sw.Write(sb.ToString());
            sw.Close();
        }
    }
烙印 2024-12-24 06:45:41

如果您想从包含这些 Unicode 字符的某个位置读取文件,然后进行一些修改并写回该文件,就会出现问题。我遇到了同样的问题。这是对我有用的解决方案

从包含 Unicode 字符的文件中读取数据

List<string>fileData= File.ReadAllLines("URPath",Encoding.Default).ToList());
//Any modifications
File.WriteAllLines("URPath", hstFileData,Encoding.Default);

我知道这不太漂亮,但它有效。
希望这有帮助!

If you want to read a file from somewhere that contains these Unicode characters then you do some modifications and write back to the file, the problem arises. I was stuck with the same problem. Here is the solution that worked for me

Reading data from a file that contains Unicode characters

List<string>fileData= File.ReadAllLines("URPath",Encoding.Default).ToList());
//Any modifications
File.WriteAllLines("URPath", hstFileData,Encoding.Default);

I know this is not pretty but it worked.
hope this helps!

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