统计英文文章的单词出现次数

在一个普通的周六早晨,一位父亲带着他的儿子在亚特兰大机场乘坐免费的穿梭列车享受乐趣。这对穿着朴素的父子,在忙碌的人群中显得格外温馨,他们简单而纯粹的快乐感染了周围的人。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class Test {
	
	public static void main(String[] args) {
		
		String tempStr = "";
		int tempbyte = 0;
		Map<String, Integer> map = new HashMap<String, Integer>();
		BufferedReader in;
		String charSet = "utf-8";
		String filePath = "c:\\file.txt";
		
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), charSet));
		}
		catch (UnsupportedEncodingException e) {
			System.out.println("不支持的字符编码:" + charSet);
			return;
		}
		catch (FileNotFoundException e) {
			System.out.println("找不到文件:" + filePath);
			return;
		}
		
		try {
			while ((tempbyte = in.read()) != -1) {
				 
				 if ((tempbyte >= 65 && tempbyte <= 90) || (tempbyte >= 97 && tempbyte <= 122) || (tempbyte == (int)'’')) {
					 tempStr += (char)tempbyte;
				 }
				 else if (!tempStr.equals("")) {
					 
					 if (map.get(tempStr) == null) {
						 map.put(tempStr, 1);
					 }
					 else {
						 map.put(tempStr, map.get(tempStr) + 1);
					 }
					 tempStr = "";
				 }
			 }
		}
		catch (IOException e) {
			System.out.println("读取文件失败");
			return;
		}
		 
		 for (Map.Entry<String, Integer> entry : map.entrySet()) {
			 System.out.println(entry.getKey() + " : " + entry.getValue());
		 }
	}
}

文章如下

A father and a son

Passing through the Atlanta airport one morning, I caught one of those trains that take travelers from the main terminal to their boarding gates. Free, sterile and impersonal, the trains run back and forth all day long. Not many people consider them fun, but on this Saturday I heard laughter.

At the front of the first car – looking out the window at the track that lay ahead – were a man and his son.


They had just stopped to let off passengers, and the doors wee closing again. “Here we go! Hold on to me tight!” the father said. The boy, about five years old, made sounds of sheer delight.

I know we’re supposed to avoid making racial distinctions these days, so I hope no one will mind if I mention that most people on the train were white, dressed for business trips or vacations – and that the father and son were black, dressed in clothes that were just about as inexpensive as you can buy.

“Look out there!” the father said to his son. “See that pilot? I bet he’s walking to his plane.” The son craned his neck to look.

As I got off, I remembered some thing I’d wanted to buy in the terminal. I was early for my flight, so I decided to go back.

I did – and just as I was about to reboard the train for my gate, I saw that the man and his son had returned too. I realized then that they hadn’t been heading for a flight, but had just been riding the shuttle.

"You want to go home now? " The father asked. 

“I want to ride some more!”

“More?” the father said, mock-exasperated but clearly pleased. “You’re not tired?”

“This is fun!” his son said.

“All right,” the father replied, and when a door opened we all got on.

There are parents who can afford to send their children to Europe or Disneyland, and the children turn out rotten. There are parents who live in million-dollar houses and give their children cars and swimming pools, yet something goes wrong. 

“Where are all these people going, Daddy?” the son asked.

“All over the world,” came the reply. The other people in the air port wee leaving for distant destinations or arriving at the ends of their journeys. The father and son, though, were just riding this shuttle together, making it exciting, sharing each other’s company.

Here was a father who cared about spending the day with his son and who had come up with this plan on a Saturday morning.

Parents who care enough to spend time, and to pay attention and to try their best. It doesn’t cost a cent, yet it is the most valuable thing in the world.

The train picked up speed, and the father pointed something out, and the boy laughed again.

保存为utf-8的文本文件格式

运行之后输出:

destinations : 1
said : 4
Where : 1
passengers : 1
time : 1
over : 1
something : 2
right : 1
black : 1
these : 2
opened : 1
asked : 2
turn : 1
going : 1
ends : 1
There : 2
he : 1
air : 1
each : 1
try : 1
go : 3
we : 3
reboard : 1
realized : 1
give : 1
A : 1
made : 1
run : 1
I : 16
The : 6
consider : 1
best : 1
reply : 1
mention : 1
caught : 1
sheer : 1
man : 2
journeys : 1
look : 1
d : 1
impersonal : 1
trips : 1
arriving : 1
a : 7
dressed : 2
come : 1
wee : 2
back : 2
front : 1
morning : 2
up : 2
picked : 1
t : 2
days : 1
s : 2
returned : 1
world : 2
yet : 2
afford : 1
to : 17
Hold : 1
supposed : 1
exasperated : 1
Disneyland : 1
Europe : 1
enough : 1
long : 1
who : 5
terminal : 2
cars : 1
care : 1
craned : 1
had : 4
trains : 2
rotten : 1
so : 2
white : 1
Atlanta : 1
fun : 2
cost : 1
pools : 1
and : 19
decided : 1
that : 8
More : 1
laughed : 1
day : 2
got : 2
early : 1
hope : 1
thing : 2
car : 1
walking : 1
can : 2
neck : 1
At : 1
distant : 1
As : 1
about : 4
children : 3
through : 1
re : 2
swimming : 1
business : 1
leaving : 1
Saturday : 2
pilot : 1
pay : 1
Here : 2
replied : 1
attention : 1
all : 3
too : 1
sounds : 1
cent : 1
houses : 1
five : 1
You : 2
speed : 1
lay : 1
ahead : 1
track : 1
at : 2
boarding : 1
as : 3
you : 1
vacations : 1
See : 1
main : 1
racial : 1
take : 1
there : 1
hadn : 1
off : 2
tight : 1
boy : 2
out : 4
doors : 1
forth : 1
Look : 1
they : 1
sterile : 1
for : 5
no : 1
shuttle : 2
door : 1
people : 4
Free : 1
avoid : 1
of : 4
airport : 1
are : 3
on : 5
inexpensive : 1
spending : 1
million : 1
or : 3
again : 2
did : 1
pointed : 1
son : 10
many : 1
not : 1
stopped : 1
now : 1
them : 1
then : 1
will : 1
tired : 1
saw : 1
years : 1
Daddy : 1
travelers : 1
some : 2
heading : 1
port : 1
home : 1
most : 2
This : 1
delight : 1
wanted : 1
looking : 1
heard : 1
riding : 2
dollar : 1
me : 1
mind : 1
want : 2
just : 5
mock : 1
let : 1
my : 2
valuable : 1
flight : 2
distinctions : 1
plane : 1
clearly : 1
laughter : 1
closing : 1
old : 1
but : 3
send : 1
buy : 2
All : 2
together : 1
though : 1
been : 2
cared : 1
doesn : 1
were : 5
more : 1
his : 7
when : 1
train : 3
Parents : 1
pleased : 1
live : 1
goes : 1
clothes : 1
gate : 1
exciting : 1
window : 1
They : 1
company : 1
bet : 1
Passing : 1
this : 3
ride : 1
parents : 2
other : 2
one : 3
from : 1
It : 1
father : 10
was : 3
sharing : 1
if : 1
making : 2
with : 2
those : 1
is : 2
it : 2
came : 1
the : 29
gates : 1
know : 1
in : 5
spend : 1
Not : 1
plan : 1
remembered : 1
wrong : 1
their : 5
first : 1


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值