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
| package Projet2.Test1;
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.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* Simple brute force implementation
*
*/
public class AnalyticsCounter implements ISymptomReader {
// proprieties
private String filepath; // attributs
private String resultsfilepath; /////////////////////
public AnalyticsCounter(String filePath, String resultsFilepath) {//////////////
this.setFilepath(filePath); ///////////////////////////////////////////////
this.setResultsfilepath(resultsFilepath); /////////////////////////////////
}
// public AnalyticsCounter(String filePath) { // constructeur de la class SymptomReader
// this.setFilepath(filePath);
// }
// getter filepath
public String getFilepath() {
return filepath;
}
// setter filepath
public void setFilepath(String filePath) {
this.filepath = filePath;
}
// getter resultsfilepath ///////////////////////////////////////
public String getResultsfilepath() { ///////////////////////////////////////
return resultsfilepath; /////////////////////////////////////////
}
// setter resultsfilepath //////////////////////////////////////////////
public void setResultsfilepath(String resultsFilePath) { /////////////////
this.resultsfilepath = resultsFilePath; //////////////////////////////
}
@Override
public Map<String, Integer> getSymptomsOccurences() { // getter
TreeMap<String, Integer> symptomReader = new TreeMap<>(); // initialisation d'une nouvelle Map
if (this.filepath != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(this.filepath));
String line = reader.readLine();
while (line != null) {
if (symptomReader.containsKey(line)) {
symptomReader.put(line, symptomReader.get(line) + 1);
} else {
symptomReader.put(line, 1);
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return symptomReader;
}
@Override
public void setMapToFile(TreeMap<String, Integer> symptomOccurence, String resultsFilePath) {
try {
File file = new File(resultsFilePath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(resultsFilePath);
BufferedWriter result = new BufferedWriter(writer);
for (String line : symptomOccurence ) {
result.write(line);
result.newLine();
}
result.close();
writer.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
} |