G级别的文本文件分割器FileSpliter

本文作者是滴水有灵,转载请保留出处:http://www.cnblogs.com/liuweizzuie/archive/2012/07/23/FileSpliter.html 

今天早上在进行例行的日志分析时,发现昨天晚上的日志文件居然有2G多,用各种工具都无法打开,为了读取里面的内容,写了一个文本分割器。不啰嗦,看代码。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Threading;
  7 
  8 namespace ConsoleApplication11
  9 {
 10     class Program
 11     {
 12         static string error_argument_null    = "错误:参数为空。";
 13         static string error_argument_length  = "错误:参数个数不正确。";
 14         static string error_argument_format  = "错误:参数{0} 不正确。";
 15         static string error_file_not_exist   = "错误:找不到文件。";
 16         static string error_file_size_input  = "错误:输入的数字不正确,必须输入一个大于0的整数。";
 17         static string error_sub_file_size    = "错误:输入的数字必须小于目标文件的大小。";
 18         static string get_file_size          = "目标文件的大小是{0:N0}字节,约合{1:N3}M, {2:N3}G。";
 19         static string hlp_msg                = "文件分割器  天擎机电 2012\r\n用法:在命令行输入:FileSpliter  {-f 要分割的文件的完整路径名| -r 要分割的文件的相对路径名}";
 20         static Exception exception           = null;
 21         static void Main(string[] args)
 22         {
 23             bool   has_error      = true;
 24             string file_full_path = string.Empty;
 25            
 26             #region 参数检查
 27             if         (args == null)          { Console.WriteLine(error_argument_null);        }
 28             else if    (args.Length != 2)      { Console.WriteLine(error_argument_length);      }
 29             else if    (args[0] != "-r" && args[0] != "-f")
 30                                                { Console.Write(error_argument_format, args[0]); }
 31             else if    (args[0] == "-r" || args[0] == "-f")
 32             {
 33                 if     (args[0] == "-f")
 34                 {
 35                     file_full_path = args[1];
 36                 }
 37                 else
 38                 {
 39                     file_full_path = Environment.CurrentDirectory + "\\" + args[1];
 40                 }
 41                 if     (File.Exists(file_full_path)) 
 42                                                { has_error = false;                             }
 43                 else                           { Console.WriteLine(error_file_not_exist);       }
 44             }
 45             else { has_error = false; }
 46             #endregion 
 47 
 48             if (has_error) { Console.WriteLine(hlp_msg); }
 49             else
 50             {
 51                 Execute(file_full_path);
 52             }
 53             Console.ReadKey();
 54         }
 55 
 56         static void Execute(string file_full_path)
 57         {
 58             string directory_path = string.Empty;
 59             string file_name = string.Empty;
 60 
 61             directory_path = Path.GetDirectoryName(file_full_path);
 62             file_name = Path.GetFileNameWithoutExtension(file_full_path);
 63 
 64             directory_path = Path.GetDirectoryName(file_full_path);
 65             file_name = Path.GetFileNameWithoutExtension(file_full_path);
 66 
 67             long file_size = 0;
 68             FileStream file_stream = new FileStream(file_full_path, FileMode.Open, FileAccess.Read);
 69             StreamReader stream_reader = new StreamReader(file_stream);
 70             file_size = file_stream.Length;
 71 
 72             long sub_file_size = 0;
 73             Console.WriteLine(get_file_size, file_size, file_size / 1024.0 / 1024, file_size / 1024.0 / 1024 / 1024);
 74             Console.Write("请输入要分割的子文件的大小(单位是M):");
 75             while (!long.TryParse(Console.ReadLine(), out sub_file_size) || sub_file_size <= 0 || (sub_file_size * 1024 * 1024) >= file_size)
 76             {
 77                 if (sub_file_size >= 0 && (sub_file_size * 1024 * 1024) >= file_size)
 78                 {
 79                     Console.WriteLine(error_sub_file_size);
 80                 }
 81                 else if (sub_file_size > 0) { break; }
 82                 else { Console.WriteLine(error_file_size_input); }
 83             }
 84 
 85             sub_file_size = sub_file_size * 1024 * 1024;
 86             long sub_file_count = file_size / sub_file_size;
 87             int cursor_top = Console.CursorTop;
 88 
 89             long sub_file_count_temp = sub_file_count;
 90             long sub_file_count_indexs = 1;
 91             while (sub_file_count_temp > 10)
 92             {
 93                 sub_file_count_temp /= 10;
 94                 sub_file_count_indexs++;
 95             }
 96 
 97             Console.CursorVisible = false;
 98             for (int i = 0; i <= sub_file_count; i++)
 99             {
100                 Console.SetCursorPosition(0, cursor_top);
101                 Console.WriteLine("正在分割{0}/{1}...", i, sub_file_count);
102                 string sub_file_name = file_name + string.Format("-{0:d" + sub_file_count_indexs.ToString() + "}.txt", i);
103                 string sub_file_full_path = Path.Combine(directory_path, sub_file_name);
104 
105                 Console.WriteLine("开始分割:" + sub_file_name + "...");
106                 Console.WriteLine("准备创建文件:" + sub_file_name);
107                 int cussor_current_top = Console.CursorTop;
108                 try
109                 {
110                     FileStream sub_file_stream = File.Open(sub_file_full_path, FileMode.Create);
111                     StreamWriter sub_writer = new StreamWriter(sub_file_stream);
112                     string str = string.Empty;
113                     while (sub_file_stream.Length < sub_file_size && !stream_reader.EndOfStream)
114                     {
115                         str = stream_reader.ReadLine();
116                         sub_writer.WriteLine(str);
117                         Console.SetCursorPosition(0, cussor_current_top);
118                         Console.WriteLine("正在写文件:{0:N0}/{1:N0}", sub_file_stream.Position, sub_file_size);
119                     }
120                     sub_writer.Close();
121                     sub_file_stream.Close();
122                 }
123                 catch (Exception e)
124                 {
125                     exception = e;
126                     break;
127                 }
128                 Console.WriteLine("分割 " + sub_file_name + " 完成。");
129             }
130             Console.CursorVisible = true;
131 
132             stream_reader.Close();
133             file_stream.Close();
134             Console.WriteLine(exception == null ? "恭喜,分割结束!" : "抱歉,分割过程中出现了错误:" + exception.Message);
135             Console.ReadKey();
136         }
137     }
138 }
posted @ 2012-07-23 13:28  滴水有灵  阅读(697)  评论(0编辑  收藏  举报