-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParallelReader.java
More file actions
executable file
·75 lines (69 loc) · 2.24 KB
/
ParallelReader.java
File metadata and controls
executable file
·75 lines (69 loc) · 2.24 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
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* Example application for BlogPost: http://wp.me/p33TyJ-fr
*
* @author GHajba
*
*/
public class ParallelReader {
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.err.println("At least two files have to be provided");
System.exit(-1);
}
String file1 = args[0];
String file2 = args[1];
wrongSolution(file1, file2);
workingSolution(file1, file2);
}
private static void wrongSolution(String file1, String file2) throws IOException {
BufferedReader br1 = null;
BufferedReader br2 = null;
try {
br1 = new BufferedReader(new FileReader(file1));
br2 = new BufferedReader(new FileReader(file2));
String line1 = "";
String line2 = "";
while ((line1 = br1.readLine()) != null || (line2 = br2.readLine()) != null) {
printLine(line1, line2);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (br1 != null) {
br1.close();
}
if (br2 != null) {
br2.close();
}
}
}
private static void workingSolution(String file1, String file2) throws IOException {
BufferedReader br1 = null;
BufferedReader br2 = null;
try {
br1 = new BufferedReader(new FileReader(file1));
br2 = new BufferedReader(new FileReader(file2));
String line1;
String line2;
while ((line1 = br1.readLine()) != null | (line2 = br2.readLine()) != null) {
printLine(line1, line2);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (br1 != null) {
br1.close();
}
if (br2 != null) {
br2.close();
}
}
}
private static void printLine(String line1, String line2) {
System.out.println((line1 == null ? "" : line1) + " | " + (line2 == null ? "" : line2));
}
}