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

时间:2022-02-01 04:15:49

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???

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

5 个解决方案

#1


29  

You never Close() the StreamWriter.

你永远不会Close()StreamWriter。

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

如果在写完后调用writer.Close(),则会看到该字符。

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

但是,因为它实现了IDisposable,所以你应该在using语句中包含StreamWriter的创建:

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

This will close the stream for you.

这将为您关闭流。

#2


5  

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.

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

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.

你可以在完成后调用Close() - 尽管我建议使用using语句来确保你的StreamWriter得到妥善处理。

string s = "آ";

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

#3


2  

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

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

#4


0  

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.
  • StreamWriter应该能够在不必选择编码的情况下编写unicode,但您可以尝试使用UTF32编码。

Check How to: Write Text to a File

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

#5


0  

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

跟随,完成导出listview的功能,以正确的特殊字符表现出色:

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();
        }
    }

#1


29  

You never Close() the StreamWriter.

你永远不会Close()StreamWriter。

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

如果在写完后调用writer.Close(),则会看到该字符。

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

但是,因为它实现了IDisposable,所以你应该在using语句中包含StreamWriter的创建:

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

This will close the stream for you.

这将为您关闭流。

#2


5  

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.

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

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.

你可以在完成后调用Close() - 尽管我建议使用using语句来确保你的StreamWriter得到妥善处理。

string s = "آ";

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

#3


2  

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

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

#4


0  

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.
  • StreamWriter应该能够在不必选择编码的情况下编写unicode,但您可以尝试使用UTF32编码。

Check How to: Write Text to a File

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

#5


0  

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

跟随,完成导出listview的功能,以正确的特殊字符表现出色:

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();
        }
    }