- class Test6
- {
- public static void main(String[] args)
- {
- String str="61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249";
- BufferedWriter bufw=null;
- try
- {
- bufw= new BufferedWriter(new FileWriter("d:\\ip.txt"));
- str=str.replaceAll("(\\d+)", "00$1");
- str=str.replace("0*(\\d{3})", "$1");
- String[] arr=str.split(" +");
- TreeSet<String> ts=new TreeSet<String>();
- for(String s :arr)
- {
- ts.add(s);
- }
- for(String ss : ts)
- {
- bufw.write(ss.replaceAll("0*(\\d+)", "$1"));
- bufw.newLine();
- bufw.flush();
- }
-
- }
- catch(Exception e)
- {
- throw new RuntimeException("故障");
- }
- finally
- {
- if(bufw!=null)
- {
- try
- {
- bufw.close();
- }
- catch(Exception e)
- {
- throw new RuntimeException("关闭流失败");
- }
- }
- }
- }
- }
复制代码题目:把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。 61.54.231.245
61.54.231.9
61.54.231.246
61.54.231.48
61.53.231.249
为什么打印的结果时这样的??我这样写有错吗?想不明白;
|