英文单词排序

想记些单词,在记事本里,乱的。 今早花了一个多小时写了个文本排
  1  using System;
  2  using System.Collections.Generic;
  3  using System.Linq;
  4  using System.Text;
  5  using System.Windows;
  6  using System.Windows.Controls;
  7  using System.Windows.Data;
  8  using System.Windows.Documents;
  9  using System.Windows.Input;
 10  using System.Windows.Media;
 11  using System.Windows.Media.Imaging;
 12  using System.Windows.Navigation;
 13  using System.Windows.Shapes;
 14  using System.IO;
 15  using Microsoft.Win32;
 16 
 17  namespace WordsSort
 18 {
 19      ///   <summary>
 20       ///  MainWindow.xaml 的交互逻辑
 21       ///   </summary>
 22       public  partial  class MainWindow : Window
 23     {
 24          public MainWindow()
 25         {
 26             InitializeComponent();
 27         }
 28 
 29          private  void btnOpen_Click( object sender, RoutedEventArgs e)
 30         {
 31             OpenFileDialog openFileDialog =  new OpenFileDialog();
 32             openFileDialog.Filter =  " Text Files|*.txt ";
 33             openFileDialog.Title =  " 打开文件 ";
 34              if (openFileDialog.ShowDialog() ==  true)
 35             {
 36                  if (File.Exists(openFileDialog.FileName))
 37                 {
 38                    Cursor oldCursor =  this.Cursor;
 39                      try
 40                     {
 41                         List< string> wordsList =  new List< string>();
 42                          using (StreamReader sr =  new StreamReader(openFileDialog.FileName, System.Text.Encoding.Default))
 43                         {
 44                              string line;
 45                              while ((line = sr.ReadLine()) !=  null)
 46                             {
 47                                  if (! string.IsNullOrEmpty(line))
 48                                 {

 50                                     wordsList.Add(line);
 51                                 }
 52                             }
 53                         }
 54                         this.Cursor = Cursors.Wait;
 55                         WriteWords(wordsList, openFileDialog.FileName);
 56 
 57                     }
 58                     catch (Exception ex)
 59                     {
 60                         throw (ex);
 61                     }
 62                     finally
 63                     {
 64                         this.Cursor = oldCursor;
 65                     }
 66 
 67                 }
 68 
 69             }
 70         }
 71 
 72         /// <summary>
 73         /// 写排序文件
 74         /// </summary>
 75         /// <param name="wordsList">单词列表</param>
 76         /// <param name="oldFileName">旧文件名</param>
 77         private void WriteWords(List<string> wordsList, string oldFileName)
 78         {
 79             int index = oldFileName.LastIndexOf('.');
 80             string newFileName = oldFileName.Remove(index) + "排序.txt";
 81             if (File.Exists(newFileName))
 82             {
 83                 File.Delete(newFileName);
 84             }
 85             wordsList.Sort();
 86             FileStream fs = new FileStream(newFileName, FileMode.Append, FileAccess.Write);
 87             string firstString = "";
 88             foreach (string word in wordsList)
 89             {
 90                 if (!string.Equals(word[0].ToString(), firstString,StringComparison.OrdinalIgnoreCase))
 91                 {
 92                     fs.Write(Encoding.Default.GetBytes("\r\n"), 02);
 93                     firstString = word[0].ToString();
 94                 }
 95 
 96                 fs.Write(Encoding.Default.GetBytes(word + "\r\n"), 0, Encoding.Default.GetByteCount(word) + 2);
 97 
 98             }
 99             fs.Close();
100         }
101 
102 
103     }
104 }

序的。

转载于:https://www.cnblogs.com/hqm850/archive/2012/03/21/2409344.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值