| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 924 人关注过本帖
标题:怎么控制台输出和写入文件不一置?
只看楼主 加入收藏
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
 问题点数:0 回复次数:2 
怎么控制台输出和写入文件不一置?
我在一个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]);//
         }
        }
    }
}
搜索更多相关主题的帖子: 控制台 文件 输出 
2008-03-25 22:22
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
得分:0 
还有一点就是写入文件的部分总是比在控制台输出的部分少一点..
我是菜鸟不知道哪个地方出了问题...
2008-03-25 22:24
lky3511814
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-3-19
收藏
得分:0 
同学介绍 你用StreamReader和StreamWriter这两个类来读写文件 很方便的
2008-03-27 20:32
快速回复:怎么控制台输出和写入文件不一置?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022274 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved