在听到这个任务的时候,脑子里一片空白,在与同学交流之后,也有了自己的一点思路,也了解了如何在Java中读取txt文件,但是在大小写转化的上还有问题
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ProjectWork1 {
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')];
}
}
isr.close();
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
}
public int[] num1()
{
return num;
}
public static void main(String[] args) {
String filePath = "D://Test.txt";
ProjectWork1 a=new ProjectWork1();
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]);
}
}
}