聊天群成员记录统计

昨天大家在k群聊天,群主谈到清理长期潜水、不冒泡的成员,于是琢磨了一个成员聊天记录统计的小程序。
从kk聊天主面板下面的喇叭 ,打开“消息管理器”页面,选中要导出的群,导出时指定聊天信息保存文件的格式为txt;在聊天窗口左上角第一个图标,点击下拉后点击‘查看通讯录’。将成员全选后,复制到一个txt文档中。
以下是程序代码,仅是简单地实现了统计的功能,未考虑性能问题。

package cn.com.ld.study.io;

/**
* @filename: QGroupUser
* @description: 群主成员
* @author lida
* @date 2013-4-9 下午2:13:55
*/
public class QGroupUser implements Comparable<QGroupUser> {
private long id;
private String name;
private int activeNo;
private boolean isAdmin;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getActiveNo() {
return activeNo;
}

public void setActiveNo(int activeNo) {
this.activeNo = activeNo;
}

public boolean isAdmin() {
return isAdmin;
}

public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}

@Override
public int compareTo(QGroupUser o) {
int result = this.activeNo - o.activeNo ;
return result < 0 ? 1 : result == 0 ? 0 : -1;
}

@Override
public String toString(){
return String.format("昵称:%s qq号:%s 聊天次数:%s 管理员:%s", this.name,this.id,this.activeNo,(this.isAdmin?"是":"否"));
}
}


package cn.com.ld.study.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QGroupMemberFilter {

// @Fields userFilePath : 群主成员
private static final String userFilePath = "d:/users.txt";

// @Fields chatMessagePath : 群聊天记录
private static final String chatMessagePath = "d:/北京Java开发.txt";

private static final String NORMAL_ADMIN = "管理员";
private static final String SUPER_ADMIN = "群主";

/**
* @Title: getQGroupUserFromFile
* @Description: 读取群主成员列表
* @return List<QGroupUser>
* @throws IOException
* 读取群主成员文件时发生io异常或error
*/
private static List<QGroupUser> getQGroupUserFromFile() throws IOException {
List<QGroupUser> qGroupUserList = new ArrayList<QGroupUser>();
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(
userFilePath)));
String content = "";
QGroupUser groupUser = null;
String userName = null;
String userid = null;
int i = 1;
boolean isAdmin = false;
while ((content = reader.readLine()) != null) {
if (content.length() == 0) {
i = 1;
groupUser = new QGroupUser();
groupUser.setId(Long.parseLong(userid));
groupUser.setName(userName);
groupUser.setAdmin(isAdmin);
qGroupUserList.add(groupUser);
isAdmin = false;
continue;
}

if (i == 1) {
userName = content;
} else if (i == 2) {
userid = content.substring(1, content.length() - 1);
} else {
// System.out.println(content.equals(ADMIN));
isAdmin = content.equals(NORMAL_ADMIN)
|| content.equals(SUPER_ADMIN) ? true : false;
}
i++;
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return qGroupUserList;
}

/**
* @Title: getUserActiveNo
* @Description: 获得指定kk号的发言次数
* @param userid
* 用户kk号
* @param chatMsg
* 读取的消息内容
* @return int 聊天次数
*/
private static int getUserActiveNo(long userid, String chatMsg) {
Pattern pattern = Pattern.compile(String.valueOf(userid));
Matcher matcher = pattern.matcher(chatMsg);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}

/**
* @Title: staUserActive
* @Description: 给群内成员设置聊天次数
* @param 群内成员
* @return void
*/
private static void staUserActive(List<QGroupUser> groupUsers)
throws IOException {
int bufSize = 1024;
byte[] bs = new byte[bufSize];
ByteBuffer byteBuf = ByteBuffer.allocate(1024);
FileChannel channel = new RandomAccessFile(chatMessagePath, "r")
.getChannel();
while (channel.read(byteBuf) != -1) {
int size = byteBuf.position();
byteBuf.rewind();
byteBuf.get(bs);
String chatMsg = new String(bs, 0, size);
for (QGroupUser user : groupUsers) {
long userCount = user.getId();
int count = getUserActiveNo(userCount, chatMsg);
user.setActiveNo(user.getActiveNo() + count);
}
byteBuf.clear();
}
}

public static void main(String[] args) throws IOException,
InterruptedException {
// 获得群成员
List<QGroupUser> groupUsers = getQGroupUserFromFile();
// 设置成员聊天次数
staUserActive(groupUsers);
// 按聊天次数降序排序
Collections.sort(groupUsers);

for (QGroupUser user : groupUsers) {
System.out.println(user.toString());
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值