java编写一个程序 统计文本文件中的字符数_编写程序,由程序的参数指定一个文本文件名,然后由程序统计并输出在该文本文件中各个单词出现的次数...

import java.util.*;

import *;

public class tongji {

/**

* Creates a new instance of tongji.

*/

public tongji() {

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

int count[]=new int[26];   //用来统计小写字母个数

int countb[]=new int[26];   //用来统计大写字母个数

byte[] b = new byte[26];

Scanner read = new Scanner(System.in);

String filename = read.nextLine();

File file = new File(filename);

String str="";

if(file.exists())

{

try{

FileInputStream fis = new FileInputStream(file);

int n=0;

while((n=fis.read(b,0,1))!=-1)

{

String s = new String(b,0,n);

str +=s;

}

}catch(IOException E)

{}

System.out.println(str);

char[] aa = str.toCharArray();

for(int i=0;i

{

if(aa[i]>=97)

{

int num = (aa[i]-'a');

count[num]++;

}

else if(aa[i]<=91&&aa[i]>=65)

{

int k = aa[i]-65;

countb[k]++;

}

}

char c='a';

for(int j=0;j<26;j++)

{

System.out.print(c+"的个数有"+count[j]);

System.out.println();

c = (char)(c+1);

}

char C = 'A';

for(int j=0;j<26;j++)

{

System.out.print(C+"的个数有"+countb[j]);

System.out.println();

C = (char)(C+1);

}

}

else

System.out.print("文件不存在");

}

}

做的不好 希望可以帮助到你 主要是思想 要利用字母的ASCII码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值