導入Java . awt . *;
導入Java . awt . event . *;
導入Java . util . vector;
導入Java . io . file;
導入Java . util . vector;
階級語言統計
{ Vector allWorsd,noSameWord
文字統計的
{ allWorsd = new Vector();
no same word = new Vector();
}
公共void字統計(文件文件)
{ try { RandomAccessFile in one = new RandomAccessFile(file,“rw“);//創建壹個對象,該對象指向文件file的inOne。
random access file int wo = new random access file(file,“rw“);//創建壹個指向文件file inTwo的對象。
long wordStarPostion = 0,wordendsposition = 0;
long length = inone . length();
int flag = 1;
int c =-1;
for(int k = 0;k & lt=長度;k++)
{ c = inone . read();// inOne調用read()方法。
boolean boo =(c & lt;=‘Z‘amp;& ampc & gt=‘A‘)| |(c & lt;=‘z‘amp;& ampc & gt=‘a’);
如果(噓)
{ if(flag = = 1)
{ wordstarposition = inone . getfile pointer()-1;
flag = 0;
}
}
其他
{ if(flag = = 0)
{
if(c = =-1)
wordendposition = inone . getfile pointer();
其他
wordendposition = inone . getfile pointer()-1;
int wo . seek(wordstarposition);// inTwo調用seek方法將讀寫位置移動到wordStarPostion。
byte cc【】= new byte【(int)wordendposition-(int)wordstarposition】;
int wo . read fully(cc);// inTwo調用read fully(byte a)方法並將cc傳遞給a。
String word =新字符串(cc);
allworsd . add(word);
如果(!(no same word . contains(word)))
no same word . add(word);
}
flag = 1;
}
}
inone . close();
int wo . close();
}
catch(異常e){ }
}
公共向量getAllWorsd()
{ return allWorsd
}
公共向量getNoSameWord()
{ return noSameWord
}
}
StatisticFrame類擴展框架實現ActionListener
語言統計數據;
TextArea showMessage
按鈕打開文件;
FileDialog打開FileDialog
向量allWord,noSameWord
公共統計框架()
{ statistic = new WordStatistic();
show message = new TextArea();
openFile =新建按鈕(“打開文件”);
openfile . addactionlistener(this);
add(openFile,BorderLayout。北);
add(show message,BorderLayout。居中);
openfiledialog = new file dialog(this,“打開文件對話框”,file dialog。負載);
all word = new Vector();
no same word = new Vector();
setSize(350,300);
set visible(true);
addWindowListener(新WindowAdapter()
{公共無效窗口關閉(WindowEvent e)
{ system . exit(0);
}
});
validate();
}
已執行公共void操作(操作事件e)
{ no same word . clear();
all word . clear();
show message . settext(null);
openfile dialog . set visible(true);
string fileName = openfile dialog . getfile();
如果(文件名!=null)
{ statistic.wordStatistic(新文件(文件名));
all word = statistic . getallworsd();
no same word = statistic . getno same word();
show message . append(“\ n“+filename+“contains“+all word . size()+“英語單詞“);
顯示消息。append(“+nosameword . size()+“不同的英語單詞“);
顯示消息。追加(“按使用頻率順序:\ n“);
int count【】= new int【no same word . size()】;
for(int I = 0;我& ltno same word . size();i++)
{ String s 1 =(String)no same word . element at(I);
for(int j = 0;j & ltall word . size();j++)
{ String S2 =(String)all word . element at(j);
if(s 1 . equals(S2))
count【I】++;
}
}
for(int m = 0;m & ltno same word . size();m++)
{ for(int n = m+1;n & ltno same word . size();n++)
{ if(count【n】& gt;計數【m】)
{ String temp =(String)no same word . element at(m);
no same word . setelementat((String)no same word . elementat(n),m);
no same word . setelementat(temp,n);
int t = count【m】;
計數【m】=計數【n】;
count【n】= t;
}
}
}
for(int m = 0;m & ltno same word . size();m++)
{ show message . append(“\ n“+(String)no same word . element at(m )+
“:“+count【m】+“/“+all word . size()+
“=“+(1.0 * count【m】)/all word . size());
}
}
}
}
公共類sy6_2
{公共靜態void main(字符串參數【】)
{ new statistic frame();
}
}