📄 filehandler.java
字号:
//package ipstatistic;
import java.io.*;
import java.util.*;
/**
* 功能:从文件中提取IP地址,并统计其出现的数,暂存在Hash表中
* @author liangwen 2006/8/1
*/
public class FileHandler extends Thread{
//当前正在操作的文件索引
private int fileIndex=0;
//文件的总数
private int fileNum;
//记录当前正处理的行号
private int lineNum=0;
//获取选择的路径文件
private File[] choosedFile;
//创建链路和节点的Hash table
private static Hashtable ipHashTable;
public FileHandler(File[] choosedFile){
//获取用户选择的输入文件
this.choosedFile=choosedFile;
//Hash表初始容量为97649,装载因子为0.75
ipHashTable=new Hashtable(97649,(float)0.75);
}
/*启动线程,依次读取每一个文件
*/
public void run(){
fileIndex = 0;
fileNum=this.choosedFile.length;
while (fileIndex < fileNum)
{
try {
lineNum=0;
StartLoading(); //装载文件
}catch (Exception e1)
{
int temp=fileIndex+1;
System.out.println("FileHandler.run(),at fileNum="
+temp+",line="+lineNum+e1.toString());
ReleaseHash();
stop();
}
fileIndex++;
IPStatisticFrame.inforOutJTA.append("第"+fileIndex
+"个文件扫描结束!\n");
}
}
/*依次处理文件中的每一行数据
*/
private void StartLoading() throws Exception
{
String bufferStr;
BufferedReader br = getBufferedReader();
if (br == null)
{
System.out.println("IOHandler.StartLoading():"+
"InputStream is null");
ReleaseHash();
stop();
}
//每次读取一行数据先存入bufferStr中
while((bufferStr = br.readLine()) != null){
lineNum++;
//IP地址最短为7个字符,如果总长度小于7,说明该行没有IP地址
if (bufferStr.length() < 7)
continue;
ParseString(new StringTokenizer(bufferStr));
}
}
/*依次处理一行数据中的每一IP地址
*/
private void ParseString(StringTokenizer tokenizer) throws Exception
{
String ipStr;
String hashKey,hashValue;
//依次处理每一节点
while(tokenizer.hasMoreTokens()) {
ipStr=tokenizer.nextToken().toString();
//判断是否为合法的IP地址
if(!IsIP(ipStr))
continue;
//将节点存入Hash表
//value格式:节点的IP地址
//key格式:该IP地址出现的次数
hashKey=ipStr;
hashValue=this.GetHashValue(this.ipHashTable,hashKey);
this.ipHashTable.put(hashKey, hashValue);
}
}
/*判断给定字符串是否为IPv4地址
*/
private boolean IsIP(String IPStr){
int length=IPStr.length();
//当前扫描的字符位置
int index=0;
//记录每个8位组中字符个数,IPv4地址中其最大取值为3
int inOctetNum=0;
//记录IPv4地址中8位组的个数,最大取值为4
int Octets=1;
//判断给定字符串是否为IPv4地址
boolean IsIP = false;
char temp;
try {
while (index < length) {
temp = IPStr.charAt(index);
if (! ( (temp >= '0' && temp <= '9') || temp == '.')) {
IsIP = false;
break;
}
if (temp == '.') {
inOctetNum = 0;
Octets++;
}
else {
inOctetNum++;
if (inOctetNum > 3) {
IsIP = false;
break;
}
}
index++;
}
}catch (Exception ex) {
System.out.println("FileHandler.IsIP():"+ex.toString());
}
if( index == length && Octets==4 && inOctetNum <=3)
IsIP=true;
return IsIP;
}
/*获取对应HashKey的HashValue,
*/
private String GetHashValue(Hashtable inforHashTable, String hashKey){
String hashValue;
int num;
try {
if(inforHashTable.containsKey(hashKey)){
//获取原始值
hashValue=inforHashTable.get(hashKey).toString();
num=Integer.parseInt(hashValue);
//原值加一
hashValue=Integer.toString(num+1);
//删除旧值
inforHashTable.remove(hashKey);
}
else
//设置初值
hashValue="1";
return hashValue;
}
catch (Exception ex) {
System.out.println("FileHandler.GetHashValue():"+ex.toString());
return null;
}
}
/*获取用于读取文件的流
*/
private BufferedReader getBufferedReader()
{
BufferedReader br = null;
try {
File file;
file = choosedFile[fileIndex];
if (!file.canRead())
System.out.println("FileHandler.getBufferedReader()"+
"can't read file");
br = new BufferedReader(
new InputStreamReader(
new FileInputStream(file)));
}
catch (FileNotFoundException e1) {
System.out.println("FileHandler.getBufferedReader():"
+e1.toString());
ReleaseHash();
stop();
}
return br;
}
/*获取路径IP地址表
*/
static Hashtable GetIPTable(){
return ipHashTable;
}
/*释放资源
*/
void ReleaseHash(){
if(!this.ipHashTable.isEmpty())
this.ipHashTable.clear();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -