C#文件读写操作实例(ReadAllText(),WriteAllLines())(Dictionary按值降序排列)(字符串忽略大小写比较、匹配)

题目:写一个能够从words.txt中读取一定单词,然后统计另一个文档text.txt中各words.txt中单词的数量,并将其写入到results.txt中。(忽略大小写)
思路
1.先读取words.txt中的内容,然后将其分割成字符串单词集合,再根据该集合创建一个键为单词,值为0的Dictionary dic。

/*分割字符串得到单词数组*/
string a = "quick is fault";
string[] b = a.Split(' ');//{"quick","is","fault"}

2.读取text.txt中的内容,将所有内容分割成单词数组textwords。将textwords中的内容与dic中的键比较,若一样,则值+1。

/*忽略大小写比较字符串是否一样*/
//返回bool值
String.Equals(string str1, string str2, StringComparision.CurrentCultureIgnoreCase);

3.得到结果集合dic,需将其按值降序排列。

/*获取按值降序集合*/
var descDic = from pair in dic orderby pair.Value descending select pair;

4.创建一个List results,按照结果文档中的格式给results添加。

foreach (var element in descDic)
	results.Add(String.Format("{0}-{1}", element.Key, element.Value));

5.将results写入results.txt。

//注意:前面千万不要在前面加上File.Create(path);否则会出现线程问题。
//File.WriteAllLines()在文件不存在时会自动创建文件。
File.WriteAllLines(path, results);

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Pro5
{
    class Program
    {
        static void Main(string[] args)
        {
            string words, text, temp, path;
            string[] keywords, textwords;
            List<string> results = new List<string>();
            Dictionary<string, int> dic = new Dictionary<string, int>();
            Console.Write("Please input the words.txt path: ");
            try
            {
                path = Console.ReadLine();
                words = File.ReadAllText(path);
                //将标点符号与空格去除提取单词
                keywords = words.Split(' ', '.', ',', '?', '!', '-', '(', ')');
                Console.Write("Please input the text.txt path: ");
                path = Console.ReadLine();
                text = File.ReadAllText(path);
                textwords = text.Split(' ', '.', ',', '?', '!', '-', '(', ')');
                for (int i = 0; i < keywords.Length; i++)
                    dic.Add(keywords[i], 0);
                for (int i = 0; i < textwords.Length; i++)
                {
                    for (int j = 0; j < dic.Count; j++)
                    {
                        temp = dic.ElementAt(j).Key;
                        if (string.Equals(temp, textwords[i], StringComparison.CurrentCultureIgnoreCase))
                            dic[temp]++;
                    }
                }
                var descDic = from pair in dic orderby pair.Value descending select pair;
                foreach (var element in descDic)
                    results.Add(String.Format("{0}-{1}", element.Key, element.Value));
                Console.Write("Please input the result.txt path: ");
                path = Console.ReadLine();
                File.WriteAllLines(path, results);
            }
            catch(Exception e)
            {
                Console.WriteLine("\r\nError!" + e.Message);
            }
            finally
            {
                Console.WriteLine("\r\nPress any key to continue...");
                Console.ReadKey();
            }
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值