富贵资源网 Design By www.hznty.com
开时始,得到的中文文件中的字符是乱码的,鸟符号看的头晕。于是就细究streamreader读取的编码格式,默认的编码是ascii,单字节的,就尝试utf8,乱码;尝试gb2312,OK!
可另一个问题又出现了,得到的两个文件的行数都不到1500行,尝试N次还是不行,很郁闷。google了下,看到try catch,就想到释放缓冲区,结果很HAPPY!
复制代码 代码如下:
private static void FnFileProcess()
{
StreamReader reader = new StreamReader(@"d:\1500.txt", Encoding.GetEncoding("GB2312"));
StreamWriter writerEn = new StreamWriter(@"d:\English.txt", false, Encoding.UTF8, 1024);
StreamWriter writerCh = new StreamWriter(@"d:\Chinese.txt", false, Encoding.UTF8, 1024);
try
{
int i = 1;
for (String line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
if (i % 2 == 1)
{
writerEn.WriteLine(line);
}
else
{
writerCh.WriteLine(line);
}
i++;
}
Console.WriteLine(i + "\tOK");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
//不清空缓冲区,得到的文件总是少几十行
writerEn.Flush();
writerCh.Flush();
}
}
可另一个问题又出现了,得到的两个文件的行数都不到1500行,尝试N次还是不行,很郁闷。google了下,看到try catch,就想到释放缓冲区,结果很HAPPY!
复制代码 代码如下:
private static void FnFileProcess()
{
StreamReader reader = new StreamReader(@"d:\1500.txt", Encoding.GetEncoding("GB2312"));
StreamWriter writerEn = new StreamWriter(@"d:\English.txt", false, Encoding.UTF8, 1024);
StreamWriter writerCh = new StreamWriter(@"d:\Chinese.txt", false, Encoding.UTF8, 1024);
try
{
int i = 1;
for (String line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
if (i % 2 == 1)
{
writerEn.WriteLine(line);
}
else
{
writerCh.WriteLine(line);
}
i++;
}
Console.WriteLine(i + "\tOK");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
//不清空缓冲区,得到的文件总是少几十行
writerEn.Flush();
writerCh.Flush();
}
}
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...