Android 简单统计文本文件字符数、单词数、行数Demo
做的demo是统计文本文件的字符数、单词数、行数的,首先呢,我们必须要有一个文本文件。所以我们要么创建一个文本文件,并保存,然后再解析;要么就提前把文本文件先放到模拟器上,然后检索到文本名再进行解析。我感觉第二种方法不可行,因为要测试时,肯定要多次测试,每次还要找到文件再修改文件内容,过于麻烦。所以我用的第一种方法,文件内容更改后直接保存即可。
首先是 页面布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.demo.MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件名称:"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件内容:"
android:textSize="15dp"/> <!--android:inputType="textMultiLine" 设置EditText可以多行输入,没有这句话也能正常运行-->
<EditText
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_content"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_write"
android:text="保存"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_analysis"
android:text="解析"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_read"
android:textSize="20dp"/>
</LinearLayout>
我是用手Android手机模拟程序的,文件保存到SD卡中,有兴趣的同学可以试试其他方法,所以我们还要在AndroidManifest.xml里加入以下权限:
<!-- SD卡中创建和删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 向SD卡写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 从SD读取数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Java代码:
保存文件部分:
要判断SD卡是否存在,是否具有读写权限,获得SD卡存储目录,保存文件,内容。
//创建文件,保存输入内容。
private void write() {
String filename=et_name.getText().toString();
String filecontent=et_content.getText().toString();
try {
if (Environment.getExternalStorageState().equals
(Environment.MEDIA_MOUNTED)) {//表明对象是否存在并具有读、写权限
//返回 File ,获取外部存储目录即 SDCard
File sdCardDir = Environment.getExternalStorageDirectory();
FileOutputStream fos = new FileOutputStream(sdCardDir.getCanonicalPath()
+ "/"+filename+".txt");//getCanonicalPath()返回的是规范化的绝对路径
fos.write(filecontent.getBytes("UTF-8"));
fos.close();//关闭输出流
Toast.makeText(this, "数据保存到"+filename+".txt"+"文件中了", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "未找到SD卡", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
}
}
解析部分:
同样要先判断SD卡是否存在,是否具有读写权限,再判断是否存在该文件,按行读取文件并解析,自加得出结果。
//解析字符数,单词数,行数,空格数
private void analysis() {
String str=""; int words = 0;//单词数
int chars = 0;//字符数
int lines = 0;//行数
int spaces=0;//空格数
int marks=0;//标点符号数
int character=0;//字母数 String filename=et_name.getText().toString();
FileInputStream fis=null;
BufferedReader br=null;
try {
//判断SD卡是否存在,并且是否具有读写权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/"+filename+".txt");
if (file.exists()){//判断文件是否存在
//打开文件输入流
fis=new FileInputStream(file);
//字符流写入了缓冲区
br=new BufferedReader(new InputStreamReader(fis)); while((str=br.readLine())!=null){//readLine()每次读取一行,转化为字符串,br.readLine()为null时,不执行 char[] b=str.toCharArray();//将字符串对象中的字符转换为一个字符数组
for (int i = 0; i < str.length(); i++) {
if (b[i]==' '){//如果字符数组中包含空格,spaces自加1
spaces++;//空格数
}else if (b[i]==','||b[i]=='.'){
marks++; }
} //单词数,split()方法,返回是一个数组,根据(空格,标点符号)分割成字符串数组,数组长度就是单词长度。
words+=str.split("[ \\.,]").length;//使用正则表达式实现多个分隔符进行分隔的效果。 chars+=str.length();//字符串的长度,即字符数,包括英文字母数+空格数+标点数
lines++;//行数(由于每次读取一行,行数自加即可)
}
character=chars-(spaces+marks);//字母数=字符数-空格数-标点数
//关闭文件
br.close(); tv_read.setText("单词数:"+words+",字符数:"+chars+",行数:"+lines+",字母数:"+character+",空格数:"+spaces+",标点符号数:"+marks);
}
else {
Toast.makeText(this, "不存在该文件", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
最后看看运算结果:

全文代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_name;
private EditText et_content;
private Button btn_write;
private Button btn_analysis;
private TextView tv_read;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
}
private void initView() {
et_name = (EditText) findViewById(R.id.et_name);
et_content = (EditText) findViewById(R.id.et_content);
btn_write = (Button) findViewById(R.id.btn_write);
btn_analysis = (Button) findViewById(R.id.btn_analysis);
tv_read = (TextView) findViewById(R.id.tv_read);
btn_write.setOnClickListener(this);
btn_analysis.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_write:
write();
btn_analysis.setClickable(true);
break;
case R.id.btn_analysis:
analysis();
break;
}
}
//创建文件,保存输入内容。
private void write() {
String filename=et_name.getText().toString();
String filecontent=et_content.getText().toString();
try {
if (Environment.getExternalStorageState().equals
(Environment.MEDIA_MOUNTED)) {//表明对象是否存在并具有读、写权限
//返回 File ,获取外部存储目录即 SDCard
File sdCardDir = Environment.getExternalStorageDirectory();
FileOutputStream fos = new FileOutputStream(sdCardDir.getCanonicalPath()
+ "/"+filename+".txt");//getCanonicalPath()返回的是规范化的绝对路径
fos.write(filecontent.getBytes("UTF-8"));
fos.close();//关闭输出流
Toast.makeText(this, "数据保存到"+filename+".txt"+"文件中了", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "未找到SD卡", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
}
}
//解析字符数,单词数,行数,空格数
private void analysis() {
String str="";
int words = 0;//单词数
int chars = 0;//字符数
int lines = 0;//行数
int spaces=0;//空格数
int marks=0;//标点符号数
int character=0;//字母数
String filename=et_name.getText().toString();
FileInputStream fis=null;
BufferedReader br=null;
try {
//判断SD卡是否存在,并且是否具有读写权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/"+filename+".txt");
if (file.exists()){//判断文件是否存在
//打开文件输入流
fis=new FileInputStream(file);
//字符流写入了缓冲区
br=new BufferedReader(new InputStreamReader(fis));
while((str=br.readLine())!=null){//readLine()每次读取一行,转化为字符串,br.readLine()为null时,不执行
char[] b=str.toCharArray();//将字符串对象中的字符转换为一个字符数组
for (int i = 0; i < str.length(); i++) {
if (b[i]==' '){//如果字符数组中包含空格,spaces自加1
spaces++;//空格数
}else if (b[i]==','||b[i]=='.'){
marks++;
}
}
//单词数,split()方法,返回是一个数组,根据(空格,标点符号)分割成字符串数组,数组长度就是单词长度。
words+=str.split("[ \\.,]").length;//使用正则表达式实现多个分隔符进行分隔的效果。
chars+=str.length();//字符串的长度,即字符数,包括英文字母数+空格数+标点数
lines++;//行数(由于每次读取一行,行数自加即可)
}
character=chars-(spaces+marks);//字母数=字符数-空格数-标点数
//关闭文件
br.close();
tv_read.setText("单词数:"+words+",字符数:"+chars+",行数:"+lines+",字母数:"+character+",空格数:"+spaces+",标点符号数:"+marks);
}
else {
Toast.makeText(this, "不存在该文件", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
知识浅薄,如有错误,还望指出。
个人项目耗时记录

Android 简单统计文本文件字符数、单词数、行数Demo的更多相关文章
- 统计文件夹下java代码行数的小程序--主要是学习任务队列的思想
首先感谢czbk的老师,录制的视频,让我们有这么好的学习资料.……—— 统计文件夹java文件的行数,首先想到的肯定是用递归的方法,因为文件夹下面可能包含文件夹,用递归的方法,代码容易写.(这和写简单 ...
- 使用Eclipse可以方便的统计工程或文件的代码行数,
使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...
- iOS 统计Xcode整个工程的代码行数
小技巧5-iOS 统计Xcode整个工程的代码行数 1.打开终端 2.cd 空格 将工程的文件夹拖到终端上,回车,此时进入到工程的路径 此时已经进入到工程文件夹下 3.运行指令 a. find . - ...
- 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。
https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表 ...
- 【原】Mac下统计任意文件夹中代码行数的工
[链接][原]Mac下统计任意文件夹中代码行数的工http://www.cnblogs.com/wengzilin/p/4580646.html
- 统计sql server 2012表的行数
--功能:统计sql server 2012表的行数 SELECT a.name, a.object_id, b.rows, b.index_id FROM sys.tables AS a INNER ...
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
1.代码的运行结果: 获取 指定文件夹下.指定文件格式 文件的: 总代码行数.总注释行数(需指定注释格式).总空行数: #coding: utf-8 import os, re # 代码所在目录 FI ...
- c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.
#include <stdio.h> #include <ctype.h> using namespace std; /* 题目:输入一行字符,分别统计出其中英文字母.空格.数 ...
- C++统计代码注释行数 & 有效代码行数 & 代码注释公共行 & 函数个数
问题来源,在14年的暑假的一次小项目当中遇到了一个这样的问题,要求统计C++代码的注释行数,有效代码行数,代码注释公共行数,以及函数个数. 下面稍微解释一下问题, 1)注释行数:指有注释的行,包括有代 ...
随机推荐
- 第一章.java&golang的区别之:闭包
对于golang一直存有觊觎之心,但一直苦于没有下定决心去学习研究,最近开始接触golang.就我个人来说,学习golang的原动力是因为想要站在java语言之外来审视java和其它语言的区别,再就是 ...
- Java基础知识回顾之七 ----- 总结篇
前言 在之前Java基础知识回顾中,我们回顾了基础数据类型.修饰符和String.三大特性.集合.多线程和IO.本篇文章则对之前学过的知识进行总结.除了简单的复习之外,还会增加一些相应的理解. 基础数 ...
- Java进阶篇设计模式之九----- 解释器模式和迭代器模式
前言 在上一篇中我们学习了行为型模式的责任链模式(Chain of Responsibility Pattern)和命令模式(Command Pattern).本篇则来学习下行为型模式的两个模式, 解 ...
- POLARDB · 最佳实践 · POLARDB不得不知道的秘密
## 前言 POLARDB作为阿里云下一代关系型云数据库,自去年9月份公测以来,收到了不少客户的重点关注,今年5月份商业化后,许多大客户开始陆续迁移业务到POLARDB上,但是由于POLARDB的很多 ...
- 8天入门docker系列 —— 第二天 通过一个aspnetcore程序加深对容器的理解
我们知道容器是一个打包了应用和相关依赖的盒子,那怎么去操控这个盒子呢? 这一篇我通过一个简单的aspnetcore程序来加深对盒子的理解,使用之前先 安装一下Docker的环境. 一:Docker的安 ...
- 从PRISM开始学WPF,Prism7更新了什么
当时我在搬运Prism6.3的sample代码的时候,就是因为网上的资料太老旧,万万没想到这给自己挖了一个坑,因为我在做笔记的时候,prism已经在更新7.0了 现在已经是7.2了,(lll¬ω¬), ...
- python3的变量作用域规则和nonlocal关键字
也许你已经觉得自己可以熟练使用python并能胜任许多开发任务,所以这篇文章是在浪费你的时间.不过别着急,我们先从一个例子开始: i = 0 def f(): print(i) i += 1 prin ...
- Android startActivity原理分析(基于Android 8.1 AOSP)
应用进程内 如何使用Intent做Activity的跳转 Intnet intent = new Intent(MainActivity.this,TestActivity.class); start ...
- Zabbix系统介绍
监控概述 硬件监控 IPMI介绍文档:http://www.ibm.com/developerworks/cn/linux/l-ipmi/ 温度:使用ipmi对硬件温度,进行监控 硬盘:使用MegaC ...
- ORM(四)字段参数
null:如果null=True,则该字段在数据库中的值可以为NULL,默认null=False. blank:如果blank=True,前端form表单可以空,注意,这与NULL不同.NULL纯粹与 ...