Split text file
GroupDocs.Merger allows to split text file into several resultant text files. The default behaviour is to split each line into separate file.
Here are the steps on how to split text file as described:
- Initialize TextSplitOptions class with output files path format, desired TextSplitMode and line numbers;
- Instantiate Merger object with source document path or stream;
- Call Split method and pass TextSplitOptions object to itfor saving resultant text files.
Split text file to several one-line files (by exact line numbers)
The following code sample demonstrates how to split text file to two one-page documents with 3rd, 6th lines from source file:
string filePath = @"c:\sample.txt";
string filePathOut = @"c:\output\line_{0}.{1}";
TextSplitOptions splitOptions = new TextSplitOptions(filePathOut, new int[] { 3, 6 });
using (Merger merger = new Merger(filePath))
{
merger.Split(splitOptions);
}
This code snippet will produce:
Text file | Line numbers |
---|---|
line_0 | 3 |
line_1 | 6 |
Split text file to several multi-line files
The following code sample demonstrates how to split text file to several multi-line files starting from 3rd and ending at 6th line numbers:
string filePath = @"c:\sample.txt";
string filePathOut = @"c:\output\text_{0}.{1}";
TextSplitOptions splitOptions = new TextSplitOptions(filePathOut, TextSplitMode.Interval, new int[] { 3, 6 });
using (Merger merger = new Merger(filePath))
{
merger.Split(splitOptions);
}
This code snippet will produce:
Text file | Line numbers |
---|---|
text_0 | 1, 2 |
text_1 | 3, 4, 5 |
text_2 | 6 |