C # version txt text separator

Source: Internet
Author: User
Tags file separator

Ha. Teacher experiment request to do file separator, here first to do a demo out and share with you.

This is the experimental requirement: 1. Capable of file segmentation
2. Split block size is determined by user input
3. Ability to merge documents
4. File segmentation and merging processes are implemented using threads
5. Data buffer must not exceed 2K
6. To have the processing progress show

The principle of the text separator is very simple, that is, a long text into a specified size of the text block, can facilitate the movement of large volumes of text.

Mainly used to FileStream, StreamReader, StreamWriter to the file read, create and other operations.

The following is the beginning of the theme: First cut the picture foreground interface is as follows:

The interface layout is as above, and you need a OpenFileDialog control to open the selection file.

Main actions:

Let's declare a few global variables:

FileStream srcfilestream;//The file stream that will be split
int fengekuaidaxiao,wenbenchangdu;//partition block size, file length
FileInfo f;//Selected Files

1, select the split file:

Code:

private void Btntextliulan_click (object sender, EventArgs e)
{
try{
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
Openfiledialog1.filter = "All Documents (*.txt) |*.txt";
Openfiledialog1.initialdirectory = "d:\\";//default turn on D disk
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 file length
}
}
Catch
{
MessageBox.Show ("Make sure that the split number you entered is correct.) ");
}
}

2, select the file to be divided, confirm the correct, split:

Split button click event Handling code

private void Btnfenge_click (object sender, EventArgs e)
{
int fengekuaishu;//partition block number
Fengekuaidaxiao = Convert.ToInt32 (Txtfengekuaidaxiao. Text);//split block size
if (wenbenchangdu% Fengekuaidaxiao > 0)//If the file length just can divide the partition block number = file length/partition block size, otherwise split block number = File length/partition block size +1;
{
Fengekuaishu = Wenbenchangdu/fengekuaidaxiao + 1;
}
Else
{
Fengekuaishu = Wenbenchangdu/fengekuaidaxiao;
}
for (int i = 0; i < Fengekuaishu i++)//Because of the need to use multithreading for file segmentation, merging, so call the new thread here
{
Thread thread = new Thread (new Parameterizedthreadstart (Fengebaocun));//thread's argument is an instance of the Parameterizedthreadstart type. The Parameterizedtheadstart parameter is a function name, which is the function that the thread will call, and if the function is a parameterless function, start the thread directly, and if it is a single parameter, pass in the argument in the start () function, and if it is a multi-parameter function, encapsulate the multiple parameters into a class, Pass Class object, here is a single parameter
Thread. Start (i);
}
MessageBox.Show ("split success");
Txtfengekuaidaxiao. Text = "";
Txtfengewenjian. Text = "";
Txtwenjiandaxiao. Text = "";
Srcfilestream.close (); Close the file stream to split
}

The thread calls the 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))/new file stream splits the file by the specified number of blocks,//The default is saved in the same directory as the source file, or you can save it yourself 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 split is complete.

Merge files below:

First select the first split file, and then enter the number of blocks to merge:

FileStream hebingfile;//new file stream to be merged
FileInfo f1;//is used to save selected file objects 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 merged file stream objects, default guaranteed and split files under one folder
}
}

Enter the number of blocks of files to be merged to merge:

private void Btnhebing_click (object sender, EventArgs e)
{
Try
{
int count = Convert.ToInt32 (Txthebingwenjianshu. Text);
for (int i = 0; i < count; i++)//And split file, the calling thread merges
{
Thread thread = new Thread (new Parameterizedthreadstart (hebing));
Thread. Start (i);
}
MessageBox.Show ("successful merger");
Txthebingwenjian. Text = "";
Hebingfile.close ();
}
Catch
{
MessageBox.Show ("Merge exception, please check and then merge again");
}
}

To perform a 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 ("merge failed");
}

}

The merge has been completed thus far.

This demo implementation of txt text file segmentation, the experiment required is the file segmentation, in fact, are almost the same.

I'll make a file split right away and share it with you.

Hope this thing can be helpful to everyone, here side mainly uses the FileStream reads, the FileInfo class, and so on commonly used file operation, as well as to the thread's call,

If you don't understand something, you can discuss it with me.

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.