Function:读取文件 @author crossoverJie Date: 05/01/2018 14:11 @since JDK 1.8
| 17 | * @since JDK 1.8 |
| 18 | */ |
| 19 | public class ReadFile { |
| 20 | private final static Logger LOGGER = LoggerFactory.getLogger(ReadFile.class); |
| 21 | |
| 22 | private static List<String> content ; |
| 23 | private static String path ="/Users/chenjie/Desktop/test.log" ; |
| 24 | |
| 25 | /** |
| 26 | * 查找关键字 |
| 27 | */ |
| 28 | private static final String KEYWORD = "login" ; |
| 29 | |
| 30 | |
| 31 | private static Map<String,Integer> countMap ; |
| 32 | /** |
| 33 | * 去重集合 |
| 34 | */ |
| 35 | private static Set<SortString> contentSet ; |
| 36 | public static void main(String[] args) { |
| 37 | contentSet = new TreeSet<>() ; |
| 38 | countMap = new HashMap<>(30) ; |
| 39 | File file = new File(path) ; |
| 40 | try { |
| 41 | //查找 |
| 42 | sortAndFindKeyWords(file); |
| 43 | |
| 44 | LOGGER.info(contentSet.toString()); |
| 45 | |
| 46 | } catch (IOException e) { |
| 47 | LOGGER.error("IOException",e); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * 查找关键字 |
| 53 | * @param file |
| 54 | * @throws IOException |
| 55 | */ |
| 56 | private static void sortAndFindKeyWords(File file) throws IOException { |
| 57 | content = Files.readLines(file, Charset.defaultCharset()); |
| 58 | LOGGER.info(String.valueOf(content)); |
| 59 | |
| 60 | for (String value : content) { |
| 61 | |
| 62 | boolean flag = value.contains(KEYWORD) ; |
| 63 | if (!flag){ |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if (countMap.containsKey(value)){ |
| 68 | countMap.put(value,countMap.get(value) + 1) ; |
| 69 | } else { |
| 70 | countMap.put(value,1) ; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | for (String key :countMap.keySet()){ |
| 75 | SortString sort = new SortString() ; |
| 76 | sort.setKey(key); |
nothing calls this directly
no outgoing calls
no test coverage detected