C#txt文本分割器
生活随笔
收集整理的這篇文章主要介紹了
C#txt文本分割器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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;//文件的大小(字節(jié))int fileLines = 0;//文本內(nèi)容的行數(shù)int setLines = 0;//自定義的行int page = 0;//分割的頁(yè)數(shù)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 = "文章段落數(shù):【" + fileLines.ToString()+ "】 段," + "文件大小:【" + fileLenth.ToString() + " 】字節(jié)。"; }}//分割按鈕的事件private void bt_cutFiles_Click(object sender, EventArgs e){if (tb_fileLocation.Text == ""){MessageBox.Show("請(qǐng)指定文件路徑!"); }else if (tb_lineNumber.Text == "0") {MessageBox.Show("請(qǐng)?jiān)O(shè)置分割的行數(shù)!");}else{cutTxt(fileLocation, int.Parse(tb_lineNumber.Text)); }}//得到行數(shù)public int getTxtProperty(string fileLocation){StreamReader sr = new StreamReader(fileLocation);string aa = "";int bb = 0;while ((aa = sr.ReadLine()) != null){bb++; }return bb;}//開(kāi)始分割public void cutTxt(string fileLocation, int lineNumbers){string [] recordLine = new string[fileLines];//定義行數(shù)大小的數(shù)組int ii = 0; string tmpLine = "";StreamReader sr = new StreamReader(fileLocation,Encoding.GetEncoding("gb2312"));while ((tmpLine = sr.ReadLine()) != null){recordLine[ii] = tmpLine;ii++;}//開(kāi)始處理分割int curLine = 0;//工作行int curPage=0;//當(dāng)前工作篇for (int p = 0; p < page-1; p++)//先寫前n篇,最后一篇單獨(dú)寫{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("當(dāng)前索引:" + curLine+"當(dāng)前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("當(dāng)前索引:" + j); }sw_Last.Flush();sw_Last.Close();MessageBox.Show("分割成功!");}//當(dāng)設(shè)置的行數(shù)更改時(shí),記錄其數(shù)值//設(shè)置文本框失效事件,在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;//獲取篇數(shù)lb_pageNum.Text = "您設(shè)置了【" + setLines + "】段為一頁(yè),預(yù)計(jì)將產(chǎn)生【" + page + "】個(gè)文件。"; }else{lb_pageNum.Text = "無(wú)法設(shè)置0個(gè)段落!";}}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){System.Diagnostics.Process.Start("mailto:liuxiangjun@188.com");}}
} View Code
實(shí)驗(yàn)要求:
1. 能進(jìn)行文件分割
2. 分割塊大小由用戶輸入決定
3. 能進(jìn)行文件合并
4. 文件分割與合并過(guò)程用線程來(lái)實(shí)現(xiàn)
5. 數(shù)據(jù)緩沖區(qū)不得超過(guò)2K
6. 要有處理進(jìn)度顯示
?
轉(zhuǎn)載于:https://www.cnblogs.com/blogpro/p/11343909.html
總結(jié)
以上是生活随笔為你收集整理的C#txt文本分割器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 27.用zxing生成二维码
- 下一篇: C#实现简单小说阅读器