日期:2014-05-20 浏览次数:20769 次
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class StringTest { public static void main(String[] args)throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("1.txt"))); TreeMap<Integer,String> maps = new TreeMap<Integer,String>(); String line = null; while((line=in.readLine())!=null){ int i = line.indexOf(":"); String str1 = line.substring(0,i); int key = Integer.parseInt(str1); String str2 = line.substring(i+1); maps.put(key, str2); } Set<Map.Entry<Integer, String>> set = maps.entrySet(); for(Map.Entry<Integer, String> entry:set){ String str = entry.getValue(); String[]s = str.split(":"); System.out.print(entry.getKey()+" "); for(String e:s){ System.out.print(e+" "); } System.out.println("\n"); } } }