C # TXT text Splitter

Source: Internet
Author: User

Ha! The instructor's experiment requires a file splitter. Here we will make a demo to share with you.

This is an experimental requirement: 1. File splitting is supported.
2. The split block size is determined by the user input.
3. Merge files
4. The process of file separation and merging is implemented by threads.
5. The data buffer cannot exceed 2 K
6. display the processing progress

The principle of the text splitter is very simple. It is to divide a long text into text blocks of a specified size, which facilitates the movement of text with a large amount of data.

It mainly uses FileStream, StreamReader, and StreamWriter to read and create files.

Next, we will start to enter the topic. The foreground page of The screenshot is as follows:

An OpenFileDialog control is also required to open the selected file.

Main Operations:

First declare the following global variables:

FileStream srcFileStream; // file stream to be split
Int fenGeKuaiDaXiao, wenBenChangDu; // split the block size and file length.
FileInfo f; // The selected file.

1. Select the split file:

Code:

Private void btntextliulan_Click (object sender, EventArgs e)
{
Try {
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
OpenFileDialog1.Filter = "all files (*. txt) | *. txt ";
OpenFileDialog1.InitialDirectory = "D :\\"; // disk D is enabled by default.
F = new FileInfo (openFileDialog1.FileName); // instantiate f, that is, the selected file

Txtfengewenjian. Text = f. FullName; // displays the full path of the selected file.
SrcFileStream = new FileStream (f. FullName, FileMode. Open, FileAccess. Read); // instantiate and Open the split file stream
StreamReader streamReader = new StreamReader (srcFileStream );
Txtfengewenjian. Text = f. FullName;
SrcFileStream = new FileStream (f. FullName, FileMode. Open, FileAccess. Read );
WenBenChangDu = (int) f. Length/1024;
Txtwenjiandaxiao. Text = wenBenChangDu. ToString (); // display the file length.
}
}
Catch
{
MessageBox. Show ("make sure that the entered split number is correct! ");
}
}

2. Select the file to be split. After confirmation, split the file:

// Split button click event processing code

Private void btnfenge_Click (object sender, EventArgs e)
{
Int fenGeKuaiShu; // Number of split Blocks
FenGeKuaiDaXiao = Convert. ToInt32 (txtfengekuaidaxiao. Text); // split the block size
If (wenBenChangDu % fenGeKuaiDaXiao> 0) // if the file length is equal to the split block size, the number of split blocks = file length/split block size, otherwise, the number of split blocks = file length/split block size + 1;
{
FenGeKuaiShu = wenBenChangDu/fenGeKuaiDaXiao + 1;
}
Else
{
FenGeKuaiShu = wenBenChangDu/fenGeKuaiDaXiao;
}
For (int I = 0; I <fenGeKuaiShu; I ++) // because multiple threads are required to split and merge files, a new thread is called.
{
Thread thread = new Thread (new ParameterizedThreadStart (FenGeBaoCun); // The Thread parameter is an instance of the ParameterizedThreadStart type. The ParameterizedTheadStart parameter is a function name, that is, the function to be called by the thread, if the function is a non-parameter function, you can directly start the thread. If it is a single-Parameter Function, You can input parameters in the start () function. If it is a multi-parameter function, encapsulate multiple parameters into a class and pass the class object. This is a single parameter.
Thread. Start (I );
}
MessageBox. Show ("split successfully ");
Txtfengekuaidaxiao. Text = "";
Txtfengewenjian. Text = "";
Txtwenjiandaxiao. Text = "";
SrcFileStream. Close (); // Close the file stream to be split
}

Thread call function:

Public void FenGeBaoCun (object I)
{
Using (FileStream fenGeFileStream = new FileStream (f. fullName. substring (0, f. fullName. length-f.Extension.Length) + I + f. extension, FileMode. openOrCreate, FileAccess. write) // the new file stream splits the file according to the specified number of parts. // by default, the file is saved in the same directory as the source file, or you can save it separately.
{
Int data = 0;

Byte [] buffer = new byte [fenGeKuaiDaXiao * 1024];
If (data = srcFileStream. Read (buffer, 0, buffer. Length)> 0)
{
BinaryWriter bw = new BinaryWriter (fenGeFileStream, Encoding. Default );
Bw. Write (buffer, 0, data );
}
Else
{
MessageBox. Show ("information not read ");
}
}

}

At this point, the file has been split.

Merge the files below:

First select the first split file, and then enter the number of parts to be merged:

FileStream heBingFile; // the new file stream to be merged
FileInfo f1; // used to save the object to be merged
Private void btnxuanzefengewenjian_Click (object sender, EventArgs e)
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
Txtfengewenjian. Text = openFileDialog1.FileName;
F1 = new FileInfo (openFileDialog1.FileName );
Txthebingwenjian. Text = f1.FullName;
HeBingFile = new FileStream (f1.FullName. substring (0, f1.FullName. length-f1.Extension.Length-1) + f1.Extension, FileMode. create, FileAccess. write); // instantiate the merged file stream object. By default, it is saved in the same folder as the split file.
}
}

Enter the number of file blocks to merge:

Private void btnhebing_Click (object sender, EventArgs e)
{
Try
{
Int count = Convert. ToInt32 (txthebingwenjianshu. Text );
For (int I = 0; I <count; I ++) // same as the split file, call the thread to merge
{
Thread thread = new Thread (new ParameterizedThreadStart (HeBing ));
Thread. Start (I );
}
MessageBox. Show ("merged ");
Txthebingwenjian. Text = "";
HeBingFile. Close ();
}
Catch
{
MessageBox. Show ("merging exception, check and merge again ");
}
}

Execute the Merge function

Public void HeBing (object I)
{
Int data = 0;
String filepath = f1.FullName. Substring (0, f1.FullName. Length-f1.Extension. Length-1) + I + f1.Extension;
FileStream fengeStream = new FileStream (filepath, FileMode. Open, FileAccess. Read );
Byte [] buffer = new byte [fengeStream. Length];
If (data = fengeStream. Read (buffer, 0, buffer. Length)> 0)
{
BinaryWriter bw = new BinaryWriter (heBingFile );
Bw. Write (buffer, 0, data );
}
Else
{
MessageBox. Show ("failed to merge ");
}

}

 

 

So far, the merge has been completed.

This demo achieves the separation of txt text files. The experiment requires the division of files, which are almost the same.

I will immediately split the file and share it with you.

I hope this will be helpful to you. Here we mainly use FileStream reading, FileInfo, and other common file operations and thread calls,

If you have any questions, you can discuss them with me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.