![JAR search and dependency download from the Maven repository](/logo.png)
net.rationalminds.util.TextFileUtility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of DateParser Show documentation
Show all versions of DateParser Show documentation
The maven main core project description
The newest version!
package net.rationalminds.util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.rationalminds.LocalDateModel;
import net.rationalminds.Parser;
public class TextFileUtility {
private static int MAX = 250;
private static Parser parser = new Parser();
/**
*
* @param textLines
* @return
* @throws IOException
*/
public static String findProbableFileDateFormat(List textLines) throws IOException {
Map map = new HashMap();
for(String line:textLines) {
try {
List dates = parser.parse(line);
for (LocalDateModel date : dates) {
String key = date.getStart() + "#" + date.getIdentifiedDateFormat();
if (map.containsKey(key)) {
int count = map.get(key) + 1;
map.put(key, count);
} else {
map.put(key, 1);
}
}
} catch (Exception ex) {
}
}
map = sortByValue(map);
if (map == null || map.isEmpty()) {
return null;
}
Entry entry = map.entrySet().iterator().next();
return entry.getKey().split("#")[1];
}
/**
*
* @param fileName
* @return
* @throws IOException
*/
public static String findProbableFileDateFormat(String fileName) throws IOException {
Map map = new HashMap();
String line = null;
BufferedReader br = new BufferedReader(new FileReader(fileName));
int lines = 0;
while ((line = br.readLine()) != null && lines < MAX) {
try {
List dates = parser.parse(line);
for (LocalDateModel date : dates) {
lines++;
String key = date.getStart() + "#" + date.getIdentifiedDateFormat();
if (map.containsKey(key)) {
int count = map.get(key) + 1;
map.put(key, count);
} else {
map.put(key, 1);
}
}
} catch (Exception ex) {
}
}
br.close();
map = sortByValue(map);
if (map == null || map.isEmpty()) {
return null;
}
Entry entry = map.entrySet().iterator().next();
return entry.getKey().split("#")[1];
}
/**
*
* @param map
* @return
*/
private static > Map sortByValue(Map map) {
List> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
Map result = new LinkedHashMap<>();
for (Map.Entry entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy