📄 fileanalyser.java
字号:
/**
* Class FileAnylser used to stastic words,figures and interpunctions in an
* english text file
*/
package file;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.io.File;
import java.util.*;
public class FileAnalyser {
//private FileReader reader;
private StreamTokenizer stok;
private File f;
private TreeMap map;
private TreeSet set;
private int words;
private int numbers;
private int interpunctions;
private String[] postfix = {"asm","bat","bak","c","cfm","cpp","doc",
"diz","h","htm","html","ihtml","ini",
"jav","java","jcp","jsp","jcw","me",
"pas","shtml","syn","tld","txt","xml","xsl"};
/**
* construct a FileAnalyser
*/
public FileAnalyser() {
map = new TreeMap();
set = new TreeSet();
for(int i = 0;i < postfix.length;i++)
set.add(postfix[i]);
}
/**
* method to stastic words,figures and interpunctions in an
* english text file
* @param fileName the file name to stastic
*/
public void stastic(String fileName) {
int tok;
String s;
words = 0;
numbers = 0;
interpunctions = 0;
try {
openFile(fileName);
FileInputStream reader = new FileInputStream(f);
stok = new StreamTokenizer(reader);
stok.ordinaryChar('.');
stok.ordinaryChar('-');
while((tok = stok.nextToken()) != stok.TT_EOF) {
if(tok == StreamTokenizer.TT_WORD) {
s = (String)stok.sval;
count(map,s);
words++;
}
else if((tok ==StreamTokenizer.TT_NUMBER)) {
s = Double.toString(stok.nval);
numbers++;
count(map,s);
}
else {
s = String.valueOf((char)stok.ttype);
count(map,s);
interpunctions++;
}
System.out.println(stok.lineno());
}
} catch(IOException ioe){
} catch(Exception e) {
}
}
/**
* private method to check a word whether is ever counted
* if is a new word put it into the map and set its value 1
* else increment the value of the word
* @param map the collection of a file to stastic,stores the words
* @param word the file encounts
*/
private void count(Map map,String word) {
Integer counter;
counter = (Integer)map.get(word);
if(counter == null) {
counter = new Integer(1);
}
else {
counter = new Integer(counter.intValue() + 1);
}
map.put(word,counter);
}
/**
* method to search how many times a word appears in the file
* @param word the word to search
* @return the times the word appears
*/
public int frequency(String word) {
Integer counter = (Integer)map.get(word);
if(counter == null) {
return 0;
}
else return counter.intValue();
}
/**
* method to open file throws FileNameEmptyException,NotTextFileException
* NoSuchFileException,IsDirectoryException
* @param fileName the file name of a file to open
*/
public void openFile(String fileName) throws FileNameEmptyException
,NotTextFileException,NoSuchFileException,IsDirectoryException {
fileName = fileName.trim();
if(fileName.length() == 0){
throw new FileNameEmptyException();
}
int dot = fileName.indexOf(".") + 1;
String s = fileName.substring(dot,fileName.length());
if(!set.contains(s))
throw new NotTextFileException(fileName);
f = new File(fileName);
if(!f.exists()) {
throw new NoSuchFileException(fileName);
}
else if(f.isDirectory())
throw new IsDirectoryException(fileName);
}
/**
* method to get the token in the file
*/
public Object[] getContent() {
return map.keySet().toArray();
}
/**
* method to get the total words in the file
*/
public int getTotalWords() {
return words;
}
/**
* method to get the total figures in the file
*/
public int getTotalNumbers() {
return numbers;
}
/**
* method to get the total interpunctions in the file
*/
public int getTotalInterpunctions() {
return interpunctions;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -