编写程序,统计英文文本文件中的字符数目和单词数目。程序运行时,输入要统计的文件的名称,程序处理后输出字符数目和单词数目。
import java.util.*;
import java.io.IOException;
import java.nio.file.*;
public class Main3{
public static void main(String [] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.println("please enter the route of the file: ");
String pathString = in.next();
Path path = Paths.get(pathString);
int numOfWords = 1;
int numOfCharacters = 0;
byte[] bytes = Files.readAllBytes(path);
//String s = String(bytes);
String[] words = new String(bytes).split("[\\s,.!?]+");
numOfWords = words.length;
numOfCharacters = bytes.length;
System.out.println("numOfCharacters : " + numOfCharacters);
System.out.println("numOfWords : " + numOfWords);
}
}