c# - 使用 Streamreader 的输入不会在 StreamWriter 中给出相同的输出

标签 c# .net filestream streamreader streamwriter

我正在使用 StreamReader 读取一个文件,然后使用 StreamWriter 将准确的行写入另一个文件。然而,我遇到的问题是,在生成的文件中,任何出现的字符“¦”都会转换为“?”。

这是初始化我的流的代码:

using (var readFile = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    using (var writeFile = new FileStream(@"Modified\" +   Path.GetFileName(path), FileMode.Create, FileAccess.Write))
    {
        using (var sr = new StreamReader(readFile, new ASCIIEncoding()))
        {
            using (var sw = new StreamWriter(writeFile, new ASCIIEncoding()))
            {
                //Read line and write it to the writer
            }
        }
    }
}

这可能是流的问题,还是更有可能是原始文件的问题?原始文件本身在多个文本编辑器中显示得很好,所以看起来是正确的。

最佳答案

¦ 不是有效的 ASCII 字符,所以我假设原始文件不是 ASCII 编码的。将编码类型更改为 UTF 可以解决问题,但很难知道这是否符合您的要求。

using (var sr = new StreamReader(readFile, new UTF8Encoding()))
{
    using (var sw = new StreamWriter(writeFile, new UTF8Encoding()))

参见 http://www.asciitable.com/ fpr 有效的 ascii 字符列表。

关于c# - 使用 Streamreader 的输入不会在 StreamWriter 中给出相同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356639/

相关文章:

java - Bouncy CaSTLe 轻量级 API 中的 CTR 操作模式?

c# - 呈现到自定义 DrawingContext

c# - 使用 C++ 调试器作为反射替代

c# - 每个目标 .NET 平台的编译是否不同?

.net - 在较旧的 Windows 系统上使用 TLS1.2

c# - 使用 c# 运行带有 activex 对象的 dll

tsql - 如何在 T-SQL 中将带有文件流的 varbinary(max) 转换为实际的 varbinary(max)

c - 如何将文本文件最后一行的内容复制到第一行? C语言编程

c# - WCF 服务返回另一个服务(服务工厂?)

c# - 读取时是否需要 Flush() FileStream?