using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;//openfiledialog using System.IO;//文本处理 namespace CutTxt { public partial class mainForm : Form { string fileLocation = "";//文件的位置 long fileLenth = 0;//文件的大小(字节) int fileLines = 0;//文本内容的行数 int setLines = 0;//自定义的行 int page = 0;//分割的页数 public mainForm() { InitializeComponent(); } //浏览按钮的事件 private void bt_browseFile_Click(object sender, EventArgs e) { OpenFileDialog open1 = new OpenFileDialog(); open1.InitialDirectory = "C:\"; open1.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*"; open1.FilterIndex = 1; open1.RestoreDirectory = false; if (open1.ShowDialog() == DialogResult.OK) { fileLocation = open1.FileName; tb_fileLocation.Text = fileLocation; tb_lineNumber.Enabled = true; tb_lineNumber.Text = "0"; fileLines = getTxtProperty(tb_fileLocation.Text); //获取文件大小 FileInfo file1 = new FileInfo(tb_fileLocation.Text); fileLenth = file1.Length; lb_txtProperty.Text = "文章段落数:【" + fileLines.ToString()+ "】 段," + "文件大小:【" + fileLenth.ToString() + " 】字节。"; } } //分割按钮的事件 private void bt_cutFiles_Click(object sender, EventArgs e) { if (tb_fileLocation.Text == "") { MessageBox.Show("请指定文件路径!"); } else if (tb_lineNumber.Text == "0") { MessageBox.Show("请设置分割的行数!"); } else { cutTxt(fileLocation, int.Parse(tb_lineNumber.Text)); } } //得到行数 public int getTxtProperty(string fileLocation) { StreamReader sr = new StreamReader(fileLocation); string aa = ""; int bb = 0; while ((aa = sr.ReadLine()) != null) { bb++; } return bb; } //开始分割 public void cutTxt(string fileLocation, int lineNumbers) { string [] recordLine = new string[fileLines];//定义行数大小的数组 int ii = 0; string tmpLine = ""; StreamReader sr = new StreamReader(fileLocation,Encoding.GetEncoding("gb2312")); while ((tmpLine = sr.ReadLine()) != null) { recordLine[ii] = tmpLine; ii++; } //开始处理分割 int curLine = 0;//工作行 int curPage=0;//当前工作篇 for (int p = 0; p < page-1; p++)//先写前n篇,最后一篇单独写 { StreamWriter sw = new StreamWriter("Xiangjun"+p+".txt", true, Encoding.GetEncoding("gb2312")); for (int j = curLine; j < curLine+setLines; j++) { sw.Write(recordLine[j]); sw.Write(" ");//写完一行后换行 } sw.Flush(); sw.Close(); curLine += setLines; curPage=p; //MessageBox.Show("当前索引:" + curLine+"当前page值:"+curPage); } //写最后一篇 StreamWriter sw_Last = new StreamWriter("Xiangjun" + (curPage+1) + ".txt", true, Encoding.GetEncoding("gb2312")); for (int j = curLine; j < fileLines; j++) { sw_Last.Write(recordLine[j]); sw_Last.Write(" ");//写完一行后换行 //MessageBox.Show("当前索引:" + j); } sw_Last.Flush(); sw_Last.Close(); MessageBox.Show("分割成功!"); } //当设置的行数更改时,记录其数值 //设置文本框失效事件,在form1.Designer.cs中 private void tb_lineNumber_LostFocus(object sender, EventArgs e) { //定义未使用 } private void tb_lineNumber_TextChanged(object sender, EventArgs e) { setLines = int.Parse(tb_lineNumber.Text); if (setLines != 0) { page = (fileLines / setLines) + 1;//获取篇数 lb_pageNum.Text = "您设置了【" + setLines + "】段为一页,预计将产生【" + page + "】个文件。"; } else { lb_pageNum.Text = "无法设置0个段落!"; } } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("mailto:liuxiangjun@188.com"); } } }
实验要求:
1. 能进行文件分割
2. 分割块大小由用户输入决定
3. 能进行文件合并
4. 文件分割与合并过程用线程来实现
5. 数据缓冲区不得超过2K
6. 要有处理进度显示
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace WindowsFormsApplication1 { public partial class 文件分割器1 : Form { //时间:2012-9-6 //作者:Olive //功能:实现大文件的分割、合并 #region Members int count1,count,fgkDaXiao; FileInfo fFenGe,fHeBing; FileStream fStream,hStream; string fenGeLuJing,heBingLuJing; int value = 0; #endregion #region Methods public 文件分割器1() { InitializeComponent(); skinEngine1.SkinFile = "WarmColor1.ssk"; } /// <summary> /// 选择要分割的文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnfgwenjian_Click(object sender, EventArgs e) { try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { fFenGe = new FileInfo(openFileDialog1.FileName); txtfenge.Text = fFenGe.FullName; fenGeLuJing = fFenGe.FullName.Substring(0, fFenGe.FullName.Length - openFileDialog1.SafeFileName.Length); txtwjdaxiao.Text = ((int)fFenGe.Length/1024).ToString(); fStream = new FileStream(fFenGe.FullName, FileMode.Open, FileAccess.Read); } } catch { MessageBox.Show("文件分割异常,请检查确认无误后再次分割!"); } } /// <summary> /// 选择要保存的路径,如果不选默认为与原文件同路径 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnfglujing_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtfgkdaxiao.Text)) { MessageBox.Show("请输入文件分割块大小!"); } if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { txtfglujing.Text= folderBrowserDialog1.SelectedPath; fenGeLuJing = folderBrowserDialog1.SelectedPath+"\"; } } /// <summary> /// 分割文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnfenge_Click(object sender, EventArgs e) { try { int wenJianLength = Convert.ToInt32(txtwjdaxiao.Text); if (wenJianLength % fgkDaXiao == 0) { count = wenJianLength / fgkDaXiao; } else { count = wenJianLength / fgkDaXiao + 1; } for (int i = 0; i < count; i++) { Thread thread = new Thread(new ParameterizedThreadStart(FenGe)); thread.Start(i); } MessageBox.Show("文件分割完毕"); } catch { MessageBox.Show("分割异常,请确认操作!"); } } /// <summary> /// 分割线程调用函数 /// </summary> /// <param name="i"></param> public void FenGe(object i) { try { using (FileStream fgstream = new FileStream(fenGeLuJing + fFenGe.Name.Substring(0, fFenGe.Name.Length - fFenGe.Extension.Length) + i + fFenGe.Extension, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = new byte[fgkDaXiao * 1024]; int data = 0; if ((data = fStream.Read(buffer, 0, buffer.Length)) > 0) { BinaryWriter bWriter = new BinaryWriter(fgstream, Encoding.Default); bWriter.Write(buffer, 0, data); value=(int)i/count*100; progressBar1.Value = value; } } } catch { MessageBox.Show("文件打开异常!"); } } /// <summary> /// 输入值变化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtfgkdaxiao_TextChanged(object sender, EventArgs e) { if (txtfgkdaxiao.Text.Length > 0) { fgkDaXiao = Convert.ToInt32(txtfgkdaxiao.Text); } } /// <summary> /// 选择要合并的文件,首个分割文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnhbwenjian_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { fHeBing = new FileInfo(openFileDialog1.FileName); txthebing.Text = fHeBing.FullName; heBingLuJing = fHeBing.FullName.Substring(0, fHeBing.FullName.Length - openFileDialog1.SafeFileName.Length) + "\"; } } /// <summary> /// 选择合并后的文件的保存路径 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnhebinglujing_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { heBingLuJing = folderBrowserDialog1.SelectedPath + "\"; } } /// <summary> /// 合并文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { try { if (count1 > 0) { using (hStream = new FileStream(heBingLuJing + fHeBing.Name.Substring(0, fHeBing.Name.Length - fHeBing.Extension.Length - 1) + fHeBing.Extension, FileMode.OpenOrCreate, FileAccess.Write)) { for (int i = 0; i < count1; i++) { Thread thread = new Thread(new ParameterizedThreadStart(HeBing)); thread.Start(i); } MessageBox.Show("合并完成"); } } else { MessageBox.Show("请输入合并文件数目"); } } catch { MessageBox.Show("合并异常,请重新合并"); } } /// <summary> /// 求要合并的文件数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txthbkuaishu_TextChanged(object sender, EventArgs e) { count1 = Convert.ToInt32(txthbkuaishu.Text); } /// <summary> /// 合并线程调用的合并函数,执行文件合并 /// </summary> /// <param name="i"></param> public void HeBing(object i) { using (FileStream readStream = new FileStream(fHeBing.FullName.Substring(0, fHeBing.FullName.Length - fHeBing.Extension.Length- 1) + i + fHeBing.Extension, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[readStream.Length]; int data = 0; if ((data = readStream.Read(buffer, 0, buffer.Length)) > 0) { BinaryWriter binary = new BinaryWriter(hStream, Encoding.Default); binary.Write(buffer, 0, data); } } } #endregion } } 本文主要用到了FileStream、FileInfo类、BinaryWriter、StreamReader、StreamWriter等常用文件操作类,而且还用到了线程的知识,如果有不懂的地方,希望可以和我交流,共同学习进步