怎么控制台输出和写入文件不一置?
我在一个WordCount的程序,最后的结果需要在控制台输出和保存在文件中,但是在两边输出的结果不一置...哪个大哥给我看一下哈...分析下原因.
using System;
using System.Collections.Generic;
using System.Text;
using
using System.Collections;
namespace WordCount
{
public class WordCount
{
private bool m_spy;
private bool m_trace;
private string m_file_name;
private string m_file_output;
private StreamReader m_reader;
private StreamWriter m_writer;
private ArrayList m_text;
private Hashtable m_words;
private string[][] m_sentences;
public void processFile()
{
openFiles();
readFile();
countWords();
writeWords();
}
private void countWords()
{
string tempStringLine;
char [] separators = {' ', '\n', '\t', // white space
'.', '\"', ';', ',', '?', '!', ')', '(', '<', '>', '[', ']' };
// Console.WriteLine("The text has: " + m_text.Count + " lines ");
m_sentences = new string[m_text.Count][];
m_words = new Hashtable();
for (int i = 0; i < m_text.Count; i++)
{
tempStringLine = (string)m_text[i];
// Console.WriteLine(m_sentences[i]);
m_sentences[i] = tempStringLine.Split(separators);
foreach (String tempString in m_sentences[i])
{
tempString.ToLower();
if (tempString.Length == 0)
{
continue;
}
string key = tempString.ToLower();
if (!m_words.Contains(key))
m_words.Add(key, 1);
else
m_words[key] = (int)m_words[key] + 1;
}
}
}
private void readFile()
{
m_file_name = @"D:\1.txt";
m_reader = File.OpenText(m_file_name);
m_text = new ArrayList();
string text_line;
while ((text_line = m_reader.ReadLine()) != null)
{
if (text_line.Length == 0)
continue;
m_text.Add(text_line);
}
Console.WriteLine();
}
private void openFiles()
{
//Console.WriteLine(" ");
;
}
private void writeWords()
{
m_writer = File.CreateText(@"D:\3.txt");
ArrayList aKeys = new ArrayList(m_words.Keys);
aKeys.Sort();
foreach (string key in aKeys)
{
m_writer.WriteLine("{0} : {1}", key, m_words[key]);//这里就是输出的位置
Console.WriteLine("{0} : {1}", key, m_words[key]);//
}
}
}
}