import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;/**
* 话费分析
*
* 使用方法:去中移动的官网上查询主叫话费详单,然后导出来,是Excel格式的,打开它,Ctrl+A,Ctrl+C,
* 然后新建个记事本文件,Ctrl+V进去,然后Ctrl+S,Alt+F4。
* 把这个记事本文件的路径和名称 放到FILE_NAME常量里面就可以了。
*
* @author dachi
* @date 20101118
*
*/
public class HfFx {
final static String FILE_NAME = "C:\\Documents and Settings\\ToShiBa\\桌面\\主叫.txt"; public static void main(String[] args) throws IOException {
zjFx();
} /**
* 主叫清单
*
* @throws IOException
*/
public static void zjFx() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
while (true) {
String line = br.readLine();
if (line != null && line.indexOf("对方号码") != -1) {
break;
}
}
Map scMap = new HashMap();
Map hfMap = new HashMap();
while (true) {
String line = br.readLine();
if (line.indexOf("费用合计") != -1) {
break;
}
if (line.length() < 3) {
continue;
}
String sjh = getValue(line, 2, "\t");
String sc = getValue(line, 5, "\t");
String hf = getValue(line, 9, "\t");
if (scMap.containsKey(sjh)) {
int oldSc = Integer.parseInt(scMap.get(sjh).toString());
scMap.put(sjh, (oldSc + Integer.parseInt(sc) + ""));
float oldHf = Float.parseFloat(hfMap.get(sjh).toString());
hfMap.put(sjh, (oldHf + Float.parseFloat(hf) + ""));
} else {
scMap.put(sjh, sc);
hfMap.put(sjh, hf);
}
}
print(hfMap, scMap);
br.close();
} /**
* @param line
* @param in 第几个标记以后
* @param str 标记:Excel复制成txt时 割断是\t
* @return
*/
private static String getValue(String line, int in, String str) {
int i = 0;
int temp = 0;
while (true) {
i++;
temp = line.indexOf(str, temp) + 1;
if (i == in) {
break;
}
}
return line.substring(temp, line.indexOf(str, temp));
}
/**
* 打印
* @param hfMap
* @param scMap
*/
private static void print(Map hfMap, Map scMap){
Iterator it = hfMap.keySet().iterator();
String[][] px = new String[hfMap.size()][2];
int i = 0;
while(it.hasNext()){
String key = it.next().toString();
px[i][0] = key;
px[i][1] = hfMap.get(key).toString();
i++;
}
for(int j = 0; j < px.length - 1; j++){
for(int k = j + 1; k < px.length; k++){
if(Float.parseFloat(px[j][1]) < Float.parseFloat(px[k][1])){
String tmp = px[j][1];
px[j][1] = px[k][1];
px[k][1] = tmp;
tmp = px[j][0];
px[j][0] = px[k][0];
px[k][0] = tmp;
}
}
}
System.out.println(" 手机号 - 时长 - 话费");
for(int j = 0; j < px.length; j++){
String sjh = px[j][0].replaceAll("17951", "");
System.out.print(b0(sjh,11));
System.out.print(" - ");
System.out.print(b0(scMap.get(px[j][0]).toString(),7));
System.out.print(" - ");
System.out.println(px[j][1]);
}
}
/**
* 补0
*/
private static String b0(String str, int ws){
while(str.length() != ws){
str = str + " ";
}
return str;
}
}
如果从登录移动官网,到分析出结果来都用程序实现就好了,模拟登录,用HTTPclient我做过,可是这种有验证码的不好弄吧。
关注验证码的问题。
分析中国移动话费清单程序---java实现