long start = System.currentTimeMillis();
File file = new File("test.txt");
Map map = new HashMap();
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bf = new BufferedReader(isr);
String str = "";
while((str = bf.readLine()) != null){
//String[] s = str.split(" ");
//System.out.println(str);
if(!"".equals(str.trim())){
String[] s = new String[2];
s[0] = str.substring(0,str.indexOf(" "));
s[1] = str.substring(str.indexOf(" ")+1);
//for (int i = 0; i < s.length; i++) {
if(s[0].indexOf("[") != -1){
String bookName = s[0].substring(1,s[0].length() - 1);
if(map.containsKey(bookName)){
String bookNames = map.get(bookName) + "," + s[1];
map.put(bookName, bookNames);
}else{
map.put(bookName, s[1]);
}
//}
}
}
}
for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
Entry type = (Entry) iterator.next();
System.out.println(type.getKey() + ":" + type.getValue());
}
long end = System.currentTimeMillis();
System.out.println(end - start);
} catch (Exception e) {
e.printStackTrace();
}
我想这个方法更好