C#txt文本分割器
生活随笔
收集整理的這篇文章主要介紹了
C#txt文本分割器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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("\r\n");//寫完一行后換行 }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("\r\n");//寫完一行后換行 //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");}}
} View Code
實驗要求:
1. 能進行文件分割
2. 分割塊大小由用戶輸入決定
3. 能進行文件合并
4. 文件分割與合并過程用線程來實現
5. 數據緩沖區不得超過2K
6. 要有處理進度顯示
?
轉載于:https://www.cnblogs.com/blogpro/p/11343909.html
總結
以上是生活随笔為你收集整理的C#txt文本分割器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 27.用zxing生成二维码
- 下一篇: socket 网络 编程