[英]Input with Streamreader doesn't give the same output in a StreamWriter
I'm reading a file with a StreamReader and writing the exact line to another file with a StreamWriter. 我正在使用StreamReader读取文件,并使用StreamWriter将确切的行写入另一个文件。 The problem I have however is that in the resulting file any occurence of the character '¦' is converted to '?'.
但问题是,在生成的文件中,任何字符“|”的出现都会转换为“?”。
This is the code for initializing my streams: 这是初始化我的流的代码:
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
}
}
}
}
Could this be a problem with the streams or is it more likely a problem with the original file? 这可能是流的问题,还是原始文件更可能出现问题? The original file itself displays just fine in multiple text editors, so it seems correct.
原始文件本身在多个文本编辑器中显示得很好,所以看起来是正确的。
¦ isn't a valid ASCII character so I'm assuming the original file isn't ASCII encoded. |不是有效的ASCII字符,因此我假设原始文件不是ASCII编码的。 Changing the encoding type to UTF would resolve the issue but it's difficult to know if that is valid for your requirements.
将编码类型更改为UTF可以解决问题,但很难知道这是否符合您的要求。
using (var sr = new StreamReader(readFile, new UTF8Encoding()))
{
using (var sw = new StreamWriter(writeFile, new UTF8Encoding()))
See http://www.asciitable.com/ fpr the valid ascii character list. 请参阅http://www.asciitable.com/fpr有效的ascii字符列表。
这是通过使用Encoding.Default而不是ASCIIEncoding来解决的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.