-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadingFile.java
More file actions
120 lines (105 loc) · 4.26 KB
/
Copy pathReadingFile.java
File metadata and controls
120 lines (105 loc) · 4.26 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.java;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ReadingFile {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "input.txt";
// This will reference one line at a time
String line = null;
try {
//This is used to check the current directory file.
File f = new File(fileName);
// FileReader reads text files in the default encoding.
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(new FileReader(f));
Map<String,Integer> mapObje = new HashMap<String,Integer>();
Map<String, List<String>> mapObject = new HashMap<>();
while((line = bufferedReader.readLine()) != null) {
String[] strArray = line.split(",");
if(mapObje.containsKey(strArray[2])) {
mapObje.put(strArray[2], mapObje.get(strArray[2]) +1);
List<String> strList = mapObject.get(strArray[2]);
strList.add(line);
mapObject.put(strArray[2], strList);
strList = null;
}else {
mapObje.put(strArray[2],1);
List<String> strList = new ArrayList<>();
strList.add(line);
mapObject.put(strArray[2], strList);
strList = null;
}
}
// Always close files.
bufferedReader.close();
List<String> listOfServerName = new ArrayList<>();
for (Map.Entry<String, Integer> entry : mapObje.entrySet()) {
if(entry.getValue() > 2) {
List<String> strList = mapObject.get(entry.getKey());
listOfServerName.add(getServerName(strList));
}
}
//Write File
// Assume default encoding.
FileWriter fileWriter =
new FileWriter("output.txt");
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
for(String str:listOfServerName) {
bufferedWriter.write(str);
}
// Always close files.
bufferedWriter.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
/**
* Gets the server name.
*
* @param strList the str list
* @return the server name
*/
private static String getServerName(List<String> strList) {
List<String> listOfServerName = new ArrayList<String>();
List<String> listOfSoftwareNames = new ArrayList<String>();
List<Double> listOfSoftwareVersions = new ArrayList<Double>();
for (Iterator iterator = strList.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
String[] strArray = string.split(",");
listOfServerName.add(strArray[0].trim());
listOfSoftwareNames.add(strArray[2].trim());
listOfSoftwareVersions.add(Double.valueOf(strArray[3].trim().substring(0, 2)));
}
int minIndex = listOfSoftwareVersions.indexOf(Collections.min(listOfSoftwareVersions));
System.out.println(minIndex + " Server Name :: " + listOfSoftwareNames.get(minIndex));
return listOfSoftwareNames.get(minIndex);
}
}
;