- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to merge two or more files alternatively into third file
Assume we have three files as −
output1.txt
Hello how are you
output2.txt
Welcome to Tutorialspoint
output3.txt
We provide simply easy learning
Example
Following Java example merges the contents of the above three files alternatively into a single file −
import java.util.Scanner; public class MergingFiles { public static void main(String args[]) throws IOException { Scanner sc1 = new Scanner(new File("D://input1.txt")); Scanner sc2 = new Scanner(new File("D://input2.txt")); Scanner sc3 = new Scanner(new File("D://input3.txt")); FileWriter writer = new FileWriter("D://result.txt"); String str[] = new String[3]; while (sc1.hasNextLine()||sc2.hasNextLine()||sc3.hasNextLine()) { str[0] = sc1.nextLine(); str[1] = sc2.nextLine(); str[2] = sc3.nextLine(); } writer.append(str[0]+"\n"); writer.append(str[1]+"\n"); writer.append(str[2]+"\n"); writer.flush(); System.out.println("Contents added "); } }
Output
Contents added
If the above three files are in the same directly you can rewrite the sample program as −
Example
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class MergingFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\example"); //List of all files and directories File filesList[] = directoryPath.listFiles(); Scanner sc = null; FileWriter writer = new FileWriter("D://output.txt"); for(File file : filesList) { sc = new Scanner(file); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); writer.append(input+"\n"); } writer.flush(); } System.out.println("Contents added "); } }
Output
Contents added
- Related Articles
- Java program to merge two files into a third file
- Merge contents of two files into a third file using C
- How to merge two strings alternatively in JavaScript
- How to merge multiple files into a new file using Python?
- Java Program to Merge two lists
- How to concatenate two files into a new file using Python?
- C# program to merge two sorted arrays into one
- Java program to merge contents of all the files in a directory
- Merging elements of two different arrays alternatively in third array in C++.
- How to Merge multiple CSV Files into a single Pandas dataframe ?
- C# program to concat two or more Lists
- Java Program to delete file or directory
- Java Program for GCD of more than two (or array) numbers
- How to Merge all CSV Files into a single dataframe – Python Pandas?
- Java Program to rename a file or directory

Advertisements