Java 中统计文件中出现单词的次数练习
统计英文article.txt文件中出现hello这个单词的次数
这个是article.txt文件内容 {
hello The Royal Navy is trying hello to play hello down the problem,
after first trying to hide it. It is clearly embarrassing.
They have hello known about the problem for hello some time
but they did not hello want it hello to get in the way of the
commissioning hello ceremony hello in front hello of the Queen. hello
}
package ltb20180106;
import java.io.*;
public class WordCount {
private File f;
private BufferedReader br;
private BufferedWriter bw;
private String readline;
private int start=0;
private int end=0;
private int count=0;
public WordCount() {
try {
f=new File("D:\\myRead\\article.txt");
br=new BufferedReader(new FileReader(f));
bw=new BufferedWriter(new OutputStreamWriter(System.out));
while((readline=br.readLine())!=null) {
start=this.readline.indexOf("hello");
if(start!=-1) { // 一行字符串中如果有hello出现就进行统计
count++;
end=this.readline.indexOf("hello",start+1);
while(end!=-1) { //如果在这行中后续还有hello出现,继续统计,直到末尾没有hello出现为止。
count++;
System.out.print("[end:"+end+"]"+",");
end=this.readline.indexOf("hello",end+1);
}
}
bw.write(readline+"\r");
System.out.println("累计总共的次数:【"+count+"】"+"start:【"+start+"】");
bw.flush();
}
System.out.println("单词hello在文章【"+f.getName()+"】中出现的次数为:【"+count+"】");
bw.close();
}catch(Exception e) {
e.getMessage();
}
}
public static void main(String[] args) {
new WordCount();
}
}
Java 中统计文件中出现单词的次数练习的更多相关文章
- Java版统计文件中的每个单词出现次数
正则表达式之Pattern和Matcher,请参见转载博客 http://www.cnblogs.com/haodawang/p/5967219.html 代码实现: import java.i ...
- linux中统计文件中一个字符串出现的次数
要统计一个字符串出现的次数,这里现提供自己常用两种方法: 1. 使用vim统计 用vim打开目标文件,在命令模式下,输入 :%s/objStr//gn 2. 使用grep: grep -o objSt ...
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...
- Java笔记13:统计文件中每个字符出现的次数
一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...
- Scala快速统计文件中特定单词,字符的个数
val fileContent=Source.fromFile("/home/soyo/桌面/ss5.txt").getLines.mkString(",") ...
- 学c语言做练习之统计文件中字符的个数
统计文件中字符的个数(采用命令行参数) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[] ...
- linux命令统计文件中某个字符串出现的次数
1.使用grep linux grep命令在我的随笔linux分类里有过简单的介绍,这里就只简单的介绍下使用grep命令统计某个文件这某个字符串出现的次数,首先介绍grep命令的几个参数,详细参数请自 ...
- excel中统计列中的值在其他列出现的次数多个条件
excel中统计列中的值在其他列出现的次数多个条件 =COUNTIFS(E2:E373,"=VIP经销商",J2:J373,K2) 解释 E列的第二行到第373行中值 等于 VIP ...
- rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少?
rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少? 结论 对于 8 来说,通过以下命令 #/usr/libexec/platform-python -c 'impor ...
随机推荐
- virtualenv搭建python3 环境
参考 1.安装python3 安装脚本如下: wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz tar zxvf Python ...
- Linux 服务器配置、运行、不用敲命令(新手必备!) - 宝塔全攻略建站一条龙
Linux 服务器配置.运行.不用敲命令 WordPress 建站攻略 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整 ...
- 配置Glassfish服务器、部署Java web项目、Maven安装配置及JDK版本匹配性问题
错误一:在win7上通过命令asadmin start-domain启动Glassfish服务器时报错(如下): Exception in thread "main" java.l ...
- Beta周第14次Scrum会议(11/23)【王者荣耀交流协会】
一.小组信息 队名:王者荣耀交流协会 小组成员 队长:高远博 成员:王超,袁玥,任思佳,王磊,王玉玲,冉华 小组照片 二.开会信息 时间:2017/11/23 17:02~17:14,总计12min. ...
- hdu2204 Eddy's爱好 打表+容斥原理
Ignatius 喜欢收集蝴蝶标本和邮票,但是Eddy的爱好很特别,他对数字比较感兴趣,他曾经一度沉迷于素数,而现在他对于一些新的特殊数比较有兴趣.这些特殊数是这样的:这些数都能表示成M^K,M和K是 ...
- Go Example--错误处理
package main import ( "errors" "fmt" ) //定义一种错误类型 type argError struct { arg int ...
- PureComponent的作用及一些使用陷阱
默认渲染行为的问题 在React Component的生命周期中,有一个shouldComponentUpdate方法.这个方法默认返回值是true. 这意味着就算没有改变组件的props或者stat ...
- Redis源码剖析和注释(七)--- 快速列表(quicklist)
Redis 快速列表(quicklist)1. 介绍quicklist结构是在redis 3.2版本中新加的数据结构,用在列表的底层实现. 通过列表键查看一下:redis 列表键命令详解 127.0. ...
- mtail 部署说明
了解一个工具最好的方式是先--help 下,看看支持的命令以及参数 启动mtail 最基本的参数: --logs 支持需要处理的log 文件,支持通过glob 模式的额查找,可以指定多次 --prog ...
- Android开发之GridView的使用(解读谷歌官方API)
GridView将图片显示在二维可移动的网格里.使用ListAdapter能够将Grid项目自己主动填充满整个布局. 以下我们依据官方文档的样例来实现一下使用GridView,点击视图里的 ...