求助!下面是一个测试读取UNIX登录登出文件解析,其中问题是解析进程不对,请高手看看!!

lixj2009 2009-05-29 01:35:28
代码如下:
public class test {

public static void main(String[] args) {
int type = -1 ;
String user = null ;
String devicename = null ;
long visittime = 0L ;
String userip = null ;
int pid = -1 ;
File file=null;
FileInputStream fis=null;
file=new File("e:\\wtmpx");
if(file==null){
return ;
}
try {
fis=new FileInputStream(file);
byte buff [] = new byte [372] ;

while(fis.read(buff)!= -1)
{
type = buff[73] ;
//检测记录类型是否是登录和登出
if( type != 7 && type != 8 )
continue ;
user = getString(buff, 0, 32 ).trim() ;
if( user.startsWith("."))
continue ;
//检测设备类型
devicename = getString(buff, 36, 32);
if( devicename.startsWith("ftp"))
continue ;
//检测完毕

//进程ID
pid=bytes2IntBE(buff,68,4);
//发生的时间

visittime= bytes2IntBE(buff, 80,4)*1000L+bytes2IntBE(buff, 84,4);
System.out.println(new Date(visittime));
System.out.printf("%tY-%<tm-%<td %<tH:%<tM:%<tS\t" , new Date(visittime));
//用户IP
userip = getString(buff,114, 257).trim() ;
System.out.println("type-->"+type);
System.out.println("user-->"+user);
System.out.println("devicename-->"+devicename);
System.out.println("pid-->"+pid);
System.out.println("userip-->"+userip);
System.out.println("visittime-->"+visittime);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static int bytes2IntBE(byte[] bys, int start, int len) {
return bytes2Int(bys, start, len, true);
}
private static int bytes2Int(byte[] bys, int start, int len,
boolean isBigEndian) {
int n = 0;
for(int i = start, k = start + len % (Integer.SIZE / Byte.SIZE + 1); i < k; i++) {
n |= (bys[i] & 0xff) << ((isBigEndian ? (k - i - 1) : i) * Byte.SIZE);
}
return n;
}
public static String getString( byte [] buff , int pos , int len )
{
rangeCheck(buff, pos) ;
rangeCheck(buff, pos+len-1) ;
byte tmp [] = new byte [len] ;
System.arraycopy(buff, pos, tmp, 0, len);
return new String(tmp) ;
}


/**
* 对数组的范围检验
*/
public static void rangeCheck( byte buff [] ,int pos )
{
if( pos >= buff.length )
throw new ArrayIndexOutOfBoundsException() ;
}

}
...全文
76 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
GeekZFZ 2009-05-29
  • 打赏
  • 举报
回复
帮顶

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧