StreamWriter Problem with encoding UTF-7
I have a text file in UTF-开发者_开发技巧7, I would like to create another file with same data like this :
using (var sw = new StreamWriter(@"D:\toto.csv.copy", false, Encoding.UTF7))
{
using (var sr = new StreamReader(@"D:\toto.csv", Encoding.UTF7))
{
string line;
while (null != (line = sr.ReadLine()))
{
sw.WriteLine(line);
}
}
}
In the original file the first line is : ¤TEST¤ In the copy file, the first line is : +AKQ-TEST+AKQ-
the original file is in UTF-7, when I watch the line variable in debug mode, I can see "¤TEST¤" in line variable.
Can you help me please ?
Many thanks
Try using a custom encoding on the writer to allow use of optional characters in the output.
UTF7Encoding allowOptionals = new UTF7Encoding(true);
using (var sw = new StreamWriter(@"D:\toto.csv.copy", false, allowOptionals))
{
using (var sr = new StreamReader(@"D:\toto.csv", Encoding.UTF7))
{
string line;
while (null != (line = sr.ReadLine()))
{
sw.WriteLine(line);
}
}
}
精彩评论