英语单词统计
能够实现单词个数统计和字母个数统计 并且将这些导入文本中
public static void main(String[] args)throws IOException//扔掉很重要
{
// 新文件
File newFile = new File("E:\\eclipse-workspace\\2.0\\src\\英语单词统计\\计算结果.txt");
int count_xiao = 0,count_da=0;
// 读取文件内容
File file = new File("E:\\eclipse-workspace\\2.0\\src\\英语单词统计\\Harry Potter.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));//读取数据
StringBuffer sb = new StringBuffer();//对字符串进行修改 并且可以进行多次修改 并且不产生新的未使用对象
String line = null;//行数
while ((line = reader.readLine()) != null) {
sb.append(line);//之间的简单相加
}
reader.close();
// 统计大小写字母个数
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);//读取第i个字母
if ((ch >= 'a' && ch <= 'z') ) {
count_xiao ++;
}
else if ( (ch >= 'A' && ch <= 'Z')) {
count_da ++;
}
}
System.out.println("小写个数:"+count_xiao);
System.out.println("大写个数:"+count_da);
//计算单词个数
int sum_dan=1;
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);//读取第i个字母
//如果有空格逗号句号空格换行冒号就是一个新的单词
if (ch<'A'||(ch>'Z'&&ch<'a')||ch>'z'){
sum_dan ++;
char c=sb.charAt(i+1);
if (c<'A'||(c>'Z'&&c<'a')||c>'z')
{
sum_dan--;
}
}
}
System.out.println("单词个数:"+sum_dan);
String s;
String[] buffer=null ;
int[] sum_geshu;
sum_geshu=new int[(int) sum_dan];
for (int i = 0; i < sb.length(); i++)
{ s=reader.readLine();
while(s!=null)
{buffer=s.split("\\s+");
}
}
reader.close();
for(int i=0;i<sum_dan;i++)
{
for(int j=i+1;j<sum_dan;j++)
{
if(buffer[i]==buffer[j])
{
sum_geshu[i]=0;
sum_geshu[i]=sum_geshu[i]+1;
buffer[j]=null;
sum_geshu[j]=0;
}
}
}
for(int i=0;i<sum_dan;i++)
{
System.out.print(buffer[i]+":"+sum_geshu[i]);
}
FileOutputStream out = new FileOutputStream(newFile);
out.write(String.valueOf(count_xiao).getBytes());
out.write(String.valueOf(count_da).getBytes());
out.write(String.valueOf(sum_dan).getBytes());
out.flush();
out.close();
}