package text6;
import java.util.*;
import java.io.File;
import java.io.FileReader;
import javax.swing.JOptionPane;
class tongji {
public static void main( String[ ] args ) throws Exception {
String str = JOptionPane.showInputDialog("请输入字符串(例如:text6.txt):");
File file = new File(str);
FileReader reader = new FileReader(file);
int length = (int)file.length();
char[] chars = new char[length];
reader.read(chars);
reader.close();
String s = String.valueOf(chars);
int count = 0;
int n = chars.length;
for(int i = 0; i < n ; i++)
{
if(chars[i] >= 'a' && chars[i] <= 'z')
{
count ++;
}
}
String[ ] words = s.replaceAll( "[^a-zA-Z]+", " " ).trim( ).split( " " );
if ( words.length > 0 ) {
TreeSet<Integer> lengths = new TreeSet<Integer>( );
for ( String word: words )
lengths.add( word.length( ) );
JOptionPane.showMessageDialog(null, "字符数:"+String.valueOf(count) +"\n"+"单词数:"+String.valueOf(words.length));
}
else
JOptionPane.showMessageDialog(null, "字符数:0"+"\n"+"单词数:0");
}
}
Java 文件统计:编写程序,统计英文文本文件中的字符数目和单词数目。程序运行时,输入要统计的文件的名称,程序处理后输出字符数目和单词数目
最新推荐文章于 2023-06-08 20:06:10 发布