C read Chinese characters and empty the buffer implementation code

  • 2020-05-12 02:26:29
  • OfStack

At the beginning, the characters in the Chinese file are garbled, the bird symbols look dizzy. Therefore, the encoding format read by streamreader was studied in detail. The default encoding was ascii, a single byte, and utf8 was tried. Try gb2312, OK!
However, another problem appeared again. The number of lines of the two files was less than 1500, so I failed to try N for several times. I was very depressed. google, try catch, I want to release the buffer, the result is 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 
{ 
// Without emptying the buffer, you will always get fewer files 10 line  
writerEn.Flush(); 
writerCh.Flush(); 
} 
} 


Related articles: