把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来
* 61.54.231.245
* 61.54.231.9
* 61.54.231.246
* 61.54.231.48
* 61.53.231.249
- TreeMap<BigInteger, String> tm = new TreeMap<>();
- BufferedReader br = new BufferedReader(new FileReader("ip.txt"));
- String line;
-
- while((line =br.readLine())!=null){
- //使用正则把数字提取出来 Tree集合
- tm.put(new BigInteger((Pattern.compile("[^0-9]").matcher(line).replaceAll(""))), line);
- }
- //遍历集合
- for (BigInteger key : tm.keySet()) {
- System.out.println(tm.get(key));
- }
复制代码
|