-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAverageFileContent.java
94 lines (68 loc) · 2.52 KB
/
AverageFileContent.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
89
90
91
92
93
94
package morphing.avg;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
public class AverageFileContentNew {
public static void main(String[] args) throws IOException {
String path = "F:\\Capstone Project\\2_New Video For Journal Only Proposed\\2_AddExtraPoints\\ThresholdDyn\\";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
// sort the array
Arrays.sort(listOfFiles, new Comparator<File>() {
@Override
public int compare(File f1, File f2) {
try {
return f1.getName().compareTo(f2.getName()) ;
} catch (NumberFormatException e) {
throw new AssertionError(e);
}
}
});
long start = System.currentTimeMillis();
String LastName = "**", CurrentName;
for (int j = 0; j < listOfFiles.length; j++) {
CurrentName = listOfFiles[j].getName();
if (j > 0) {
String input1 = path + LastName;
String input2 = path + CurrentName;
BufferedReader br1 = new BufferedReader(new FileReader(input1));
BufferedReader br2 = new BufferedReader(new FileReader(input2));
String line1;
String line2;
String line1_element1, line1_element2;
String line2_element1, line2_element2;
String filename1 = LastName.substring(0, LastName.lastIndexOf("."));
String filename2 = CurrentName.substring(0, CurrentName.lastIndexOf("."));
String p = "F:\\Capstone Project\\2_New Video For Journal Only Proposed\\3_FileAvg\\ThresholdDyn\\";
String FILENAME = p + "avg_" + filename1 + "_" + filename2 + ".txt";
FileWriter fw = new FileWriter(FILENAME);
BufferedWriter bw = new BufferedWriter(fw);
while (true) {
line1 = br1.readLine();
line2 = br2.readLine();
if (line1 == null || line2 == null)
break;
line1_element1 = line1.split(" ")[0];
line1_element2 = line1.split(" ")[1];
line2_element1 = line2.split(" ")[0];
line2_element2 = line2.split(" ")[1];
int average1 = (Integer.valueOf(line1_element1) + Integer.valueOf(line2_element1)) / 2;
int average2 = (Integer.valueOf(line1_element2) + Integer.valueOf(line2_element2)) / 2;
String content = average1 + " " + average2;
bw.write(content + "\n");
}
// System.out.println(FILENAME + "Created");
br1.close();
br2.close();
bw.close();
}
LastName = CurrentName;
}
System.out.println("Total Time Taken: " + (System.currentTimeMillis() - start));
}
}