-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeTwoFiles.java
88 lines (80 loc) · 1.92 KB
/
MergeTwoFiles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.javamultiplex.filehandling;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MergeTwoFiles {
public static void main(String[] args) throws IOException {
Scanner input=null;
PrintWriter out=null;
BufferedReader br=null;
try
{
input=new Scanner(System.in);
System.out.println("Enter first file with extenstion : ");
String file1=input.nextLine();
System.out.println("Enter second file with extenstion : ");
String file2=input.nextLine();
System.out.println("Enter third file with extension : ");
String file3=input.nextLine();
String temp=null;
if(isValidFileName(file1) && isValidFileName(file2) && isValidFileName(file3))
{
//Reading first file.
FileReader fr=new FileReader(file1);
br=new BufferedReader(fr);
//Creating third file.
out=new PrintWriter(file3);
temp=br.readLine();
while(temp!=null)
{
out.println(temp);
temp=br.readLine();
}
//Reading second file.
fr=new FileReader(file2);
br=new BufferedReader(fr);
temp=br.readLine();
while(temp!=null)
{
out.println(temp);
temp=br.readLine();
}
System.out.println("Files has been merged successfully");
}
else
{
System.out.println("Please enter file name correctly.");
}
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
}
finally
{
if(input!=null)
{
input.close();
}
if(out!=null)
{
out.close();
}
if(br!=null)
{
br.close();
}
}
}
private static boolean isValidFileName(String fileName) {
String pattern = "^.+\\..+$";
boolean result = false;
if (fileName.matches(pattern)) {
result = true;
}
return result;
}
}