Split text file

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Merger MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-merger -- dnx GroupDocs.Merger.Mcp --yes
Folders, licensing and more →

GroupDocs.Merger allows to split text file into several resultant text files. The default behavior is to split each line into separate file.
Here are the steps on how to split text file as described:

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 });

Merger merger = new Merger(filePath);
merger.split(splitOptions);

This code snippet will produce:

Text fileLine numbers
line_03
line_16

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 });

Merger merger = new Merger(filePath);
merger.split(splitOptions);

This code snippet will produce:

Text fileLine numbers
text_01, 2
text_13, 4, 5
text_26