開時始,得到的中文文件中的字符是亂碼的,鳥符號看的頭暈。於是就細究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();
}
}