Merge text files

A file with .TXT extension represents a text document that contains plain text in the form of lines. Paragraphs in a text document are recognized by carriage returns and are used for better arrangement of file contents. A standard text document can be opened in any text editor or word processing application on different operating systems. All the text contained in such a file is in human-readable format and represented by sequence of characters.

How to merge text files in Java

When it’s required to organize several text files into one file it’s possible to solve the task with help of GroupDocs.Merger only and no need to use any third-party software.

The workflow of combining files together is quite simple:

  • Create an instance of Merger class and pass source TXT file path as a constructor parameter. You may specify absolute or relative file path as per your requirements.
  • Add another TXT file to merge with join method. Repeat this step for other TXT documents you want to merge.
  • Call Merger class save method and specify the filename for the merged TXT file as parameter.

The following example demonstrates how to merge TXT files with several lines of java code:

// Load the source TXT file
Merger merger = new Merger("c:\sample1.txt")

// Add another TXT file to merge
merger.join("c:\sample2.txt");
// Merge TXT files and save result
merger.save("c:\merged.txt");

Code Examples

Please find more use-cases and complete Java sources of our backend and frontend examples and try them for free!

Merge TXT Live Demo

GroupDocs.Merger for Java provides an online TXT Merger App, which allows you to try it for free and check its quality and accuracy.