📄 indexsearch.java
字号:
package jp.co.sjts.gsession.tools;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
/**
* 僀儞僨僢僋僗婎杮僋儔僗 IndexSearch.java
* Copyright (C) 1999-2000 Japan Total System Co,LTD
*
* @author Satoru K <koni@sjts.co.jp>
*/
abstract public class IndexSearch
{
private File dir;
abstract protected String charTrans(char c);
public IndexSearch(File dir)
{
this.dir = dir;
}
public IndexSearch(String dir)
{
this.dir = new File(dir);
}
/**
* 懚嵼偡傞僀儞僨僢僋僗偺儅僢僾傪庢摼偡傞
*
* @return 懚嵼偡傞Index(hashcode)
*/
public Map getIndex()
{
if(!dir.exists())
dir.mkdirs();
if(!dir.isDirectory())
return null;
String[] filesName = dir.list(new FileFileFilter());
Map nummap=new HashMap(filesName.length);
for(int i=0;i<filesName.length;i++)
{
try {
nummap.put(new Integer(filesName[i]),new Integer(filesName[i]));
}catch(NumberFormatException e){
return null;
}
}
return nummap;
}
/**
* 奩摉偡傞儐乕僓ID偺儕僗僩傪庢摼偡傞
*
* @param index 僼傽僀儖柤(hashcode)
* @return 奩摉偡傞儐乕僓ID
*/
public List getIDs(int index) throws GSException
{
if(index<0)
return null;
File file = new File(dir,Integer.toString(index));
if( (!file.exists())||(!file.isFile()))
return null;
String buf;
List list = new ArrayList();
BufferedReader br = GSTextFile2.OpenLoad(file);
try {
for(buf=br.readLine();buf!=null;buf=br.readLine())
list.add(new Integer(buf));
br.close();
} catch(IOException e) {
throw new GSException("IO僄儔乕偱偡",e);
}
return list;
}
/**
* 儐乕僓傪僀儞僨僢僋僗傊捛壛偟傑偡
*
* @param uid 捛壛偡傞儐乕僓ID
* @param idxStr 僀儞僨僢僋僗偵偡傞暥帤(愭摢1暥帤栚偑僀儞僨僢僋僗偲側傞)
* @return 僀儞僨僢僋僗偺僴僢僔儏僐乕僪
*/
public String addIdx(int uid,String idxStr) throws GSException
{
// 僨傿儗僋僩儕偑側偄応崌偼丄嶌惉偡傞
if(!dir.exists())
dir.mkdirs();
String one=" ";
if( (idxStr!=null)&&(idxStr.length()>0) )
one = charTrans(idxStr.charAt(0));
else
one = charTrans('#');
String hash = Integer.toString(one.hashCode());
File file = new File(dir,hash);
List list=null;
if( (file.exists())&&(file.isFile()))
list = getIDs(one.hashCode());
if(list==null)
list = new ArrayList();
list.add(new Integer(uid));
PrintWriter pw = GSTextFile2.OpenSave(file);
Iterator it = list.iterator();
while(it.hasNext())
{
Integer i = (Integer)it.next();
pw.println(i.toString());
}
pw.flush();
pw.close();
return hash;
}
/**
* 儐乕僓傪僀儞僨僢僋僗偐傜嶍彍偟傑偡
*
* @param uid 嶍彍偡傞儐乕僓ID
* @param idxStr 僀儞僨僢僋僗偵偡傞暥帤(愭摢1暥帤栚偑僀儞僨僢僋僗偲側傞)
*/
public void delIdx(int uid,String idxStr) throws GSException
{
String one;
if( (idxStr!=null)&&(idxStr.length()>0) )
one = charTrans(idxStr.charAt(0));
else
one = charTrans('#');
File file = new File(dir,Integer.toString(one.hashCode()));
List list=null;
if( (file.exists())&&(file.isFile()))
list = getIDs(one.hashCode());
else
return;
list.remove(new Integer(uid));
if(list.size()>0){
PrintWriter pw = GSTextFile2.OpenSave(file);
Iterator it = list.iterator();
while(it.hasNext())
{
Integer i = (Integer)it.next();
pw.println(i.toString());
}
pw.flush();
pw.close();
}else{
file.delete();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -