-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoping_file.java
More file actions
42 lines (31 loc) · 1.04 KB
/
Copy pathcoping_file.java
File metadata and controls
42 lines (31 loc) · 1.04 KB
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
import java.io.*;
public class coping_file {
// Java program to merge two
// files into third file
public static void main(String[] args) throws IOException {
// PrintWriter object for file3.txt
PrintWriter pw = new PrintWriter("file3.txt");
// BufferedReader object for file1.txt
BufferedReader br = new BufferedReader(new FileReader("file1.txt"));
String line = br.readLine();
// loop to copy each line of
// file1.txt to file3.txt
while (line != null) {
pw.println(line);
line = br.readLine();
}
br = new BufferedReader(new FileReader("file2.txt"));
line = br.readLine();
// loop to copy each line of
// file2.txt to file3.txt
while (line != null) {
pw.println(line);
line = br.readLine();
}
pw.flush();
// closing resources
br.close();
pw.close();
System.out.println("Merged file1.txt and file2.txt into file3.txt");
}
}