一、问题描述
给定一个文本文档(.txt文件),编写一个Java程序,读取并输出该文本文档的单词数(单词之间用空格分隔)、句子数(句子用一般标点符号 !?.: 分隔)、字符数(包括字母、数字、空格、标点)、段落数(段与段之间有一空行)和空格数,共5行输出。
问题难度系数:3/5
二、Java实现
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
File file = new File("./Input.txt");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;
while ((line = bufferedReader.readLine()) != null)
{
if (line.equals(""))
{
paraCount += 1;
}
else
{
characterCount += line.length();
String[] words = line.split("\\s+");
wordCount += words.length;
whiteSpaceCount += wordCount - 1;
String[] sentence = line.split("[!?.:]+");
sentenceCount += sentence.length;
}
}
if (sentenceCount >= 1)
{
paraCount ++;
}
System.out.println("Total word count = " + wordCount);
System.out.println("Total number of sentences = " + sentenceCount);
System.out.println("Total number of characters = " + characterCount);
System.out.println("Number of paragraphs = " + paraCount);
System.out.println("Total number of whitespaces = " + whiteSpaceCount);
}
}
三、样例输入、输出
样例输入(文本文档内容)
Donald Hardman (21 February 1899 – 2 March 1982) was a senior Royal Air Force (RAF) commander. He joined the Royal Flying Corps in 1917 and flew fighters over the Western Front, achieving nine victories to become a decorated ace. Between the wars he saw service with RAF squadrons in India and Egypt. At the outbreak of World War II, Hardman was a wing commander, attached to the Air Ministry. In 1944 he commanded No. 232 (Transport) Group during the Burma campaign. He served successively as Assistant Chief of the Air Staff, Commandant of RAF Staff College, Bracknell, and Air Officer Commanding-in-Chief of RAF Home Command. He was knighted in 1952. Hardman was Chief of the Air Staff of the Royal Australian Air Force from 1952 to 1954, and was responsible for reorganising its geographically based command-and-control system into a functional structure. After returning to Britain, he joined the Air Council in May 1954, and was promoted to air chief marshal the following year. He retired from the RAF in 1958.
样例输出
总单词数:171
总句子数:11
总字符数:1017
总段落数:1
总空格数:170
四、本文涉及知识点
1. Java 文件读取 操作;文件流FileInputStream类;按行读取文件 readLine() 方法;
2. 字符串 操作:求长度 .length() 方法、字符串分割 .split() 方法、字符串判等 .equals() 方法
3. Java文件输入输出类 Java.io.* ;输入输出异常类 IOException ;简单输入、输出操作。