统计英文字母出现次数
源码:
package yingwen;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class yingwen {
private int num[]=new int[52];
public void readTxt(String filePath) {
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class yingwen {
private int num[]=new int[52];
public void readTxt(String filePath) {
try {
File file = new File(filePath);
if(file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
while(isr.ready())
{
char c=(char)isr.read();
if(c>='a'&&c<='z')
{
++num[(int)(c-'a')];
}
else if(c>='A'&&c<='Z')
{
++num[(int)(c-'A')];
}
}
// BufferedReader br = new BufferedReader(isr);
// String lineTxt = null;
// while ((lineTxt = br.readLine()) != null) {
// System.out.println(lineTxt);
// }
// br.close();
isr.close();
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
File file = new File(filePath);
if(file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
while(isr.ready())
{
char c=(char)isr.read();
if(c>='a'&&c<='z')
{
++num[(int)(c-'a')];
}
else if(c>='A'&&c<='Z')
{
++num[(int)(c-'A')];
}
}
// BufferedReader br = new BufferedReader(isr);
// String lineTxt = null;
// while ((lineTxt = br.readLine()) != null) {
// System.out.println(lineTxt);
// }
// br.close();
isr.close();
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
}
public int[] num1()
{
return num;
}
public int[] num1()
{
return num;
}
public static void main(String[] args) {
String filePath = "E:\\Yingwen\\English.txt";
yingwen a=new yingwen();
a.readTxt(filePath);
int num[]=a.num1();
for(int i=0;i<52;i++)
{
if(i<=25)
System.out.println((char)(i+'a')+" "+num[i]);
else System.out.println((char)(i+'A'-26)+" "+num[i]);
}
}
}
String filePath = "E:\\Yingwen\\English.txt";
yingwen a=new yingwen();
a.readTxt(filePath);
int num[]=a.num1();
for(int i=0;i<52;i++)
{
if(i<=25)
System.out.println((char)(i+'a')+" "+num[i]);
else System.out.println((char)(i+'A'-26)+" "+num[i]);
}
}
}