📄 wordcounterfile.java
字号:
/** * <p>Title: StandBayeMail </p> * <p>Description: A bayesian spam filter</p> * <p>Copyright: Copyright (c) 2004 by Luca M. Viola</p> * <p>Company: 3AM.it</p> * @author Luca M. Viola <luca@3am.it> * @version 1.0 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package StandBayeMail;import java.io.*;import java.util.*;public class WordCounterFile{ private HashMap table; private String filename; private int mboxes; private Iterator it; private boolean update; public WordCounterFile( String filename,boolean update ) { this.filename=filename; this.update=update; load(); } public WordCounterFile() { HashMap table=new HashMap(); this.table=table; } private void load() { HashMap table=new HashMap(); Properties prop=new Properties(); try { File f=new File(filename); if( f.exists() ) { FileInputStream is = new FileInputStream(filename); prop.load(is); is.close(); } else System.err.println("Warning: database file "+filename+" not found, it will be created"); } catch( IOException ioe ) { ioe.printStackTrace(); this.table=table; return; } Enumeration en=prop.keys(); while( en.hasMoreElements() ) { String key=(String)en.nextElement(); String value=prop.getProperty(key,"0"); Integer _value; try { _value=new Integer(value); } catch( NumberFormatException e) { _value=null; } table.put(key,_value); } this.table=table; } public void emptykey( String key ) { table.put(key,"0"); } public void increment( String key ) { Integer value=(Integer)table.get(key); if( value==null ) value=new Integer(0); int _value=value.intValue(); _value++; Integer newvalue=new Integer(_value); table.put(key,newvalue); } public int getCount( String key ) { Integer value=(Integer)table.get(key); if( value==null ) value=new Integer(0); int _value=value.intValue(); return _value; } public int getKeyNumber() { return table.size(); } public void initIterator() { it=table.keySet().iterator(); } public int getMboxesCount() {// Integer _n=(Integer)table.get("__NUMMBOXES__"); int n=0; try {// n=Integer.parseInt(_n); n=((Integer)table.get("__NUMMBOXES__")).intValue(); } catch( NumberFormatException e) { } return n; } public String nextKey() { if( it!=null ) if( it.hasNext() ) { String key=(String)it.next(); return key; } return null; } public void commit(int mboxes) { this.mboxes=mboxes; Properties prop=new Properties(); int oldnum=0; Integer _oldnum=((Integer)table.get("__NUMMBOXES__")); if( _oldnum!=null ) oldnum=_oldnum.intValue(); mboxes+=oldnum; table.put("__NUMMBOXES__",new Integer(mboxes)); initIterator(); while( it.hasNext() ) { String key=(String)it.next(); Integer value=(Integer)table.get(key); prop.put(key,value.toString()); } try { FileOutputStream out=new FileOutputStream( filename,update ); prop.store(out,"#-- Generated by StandBayeMail -- PLEASE DO NOT EDIT THIS FILE"); out.close(); } catch( IOException ioe ) { ioe.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -