做的demo是统计文本文件的字符数、单词数、行数的,首先呢,我们必须要有一个文本文件。所以我们要么创建一个文本文件,并保存,然后再解析;要么就提前把文本文件先放到模拟器上,然后检索到文本名再进行解析。我感觉第二种方法不可行,因为要测试时,肯定要多次测试,每次还要找到文件再修改文件内容,过于麻烦。所以我用的第一种方法,文件内容更改后直接保存即可。

首先是 页面布局:

  1. <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">
  2.  
  3. <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"/>
  4.  
  5. <!--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>
  1.  

我是用手Android手机模拟程序的,文件保存到SD卡中,有兴趣的同学可以试试其他方法,所以我们还要在AndroidManifest.xml里加入以下权限:

  1. <!-- SD卡中创建和删除文件的权限 -->
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  3. <!-- 向SD卡写入数据的权限 -->
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  5. <!-- 从SD读取数据权限 -->
  6. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Java代码:

保存文件部分:

要判断SD卡是否存在,是否具有读写权限,获得SD卡存储目录,保存文件,内容。

  1. //创建文件,保存输入内容。
  2. private void write() {
  3. String filename=et_name.getText().toString();
  4. String filecontent=et_content.getText().toString();
  5. try {
  6. if (Environment.getExternalStorageState().equals
  7. (Environment.MEDIA_MOUNTED)) {//表明对象是否存在并具有读、写权限
  8. //返回 File ,获取外部存储目录即 SDCard
  9. File sdCardDir = Environment.getExternalStorageDirectory();
  10. FileOutputStream fos = new FileOutputStream(sdCardDir.getCanonicalPath()
  11. + "/"+filename+".txt");//getCanonicalPath()返回的是规范化的绝对路径
  12. fos.write(filecontent.getBytes("UTF-8"));
  13. fos.close();//关闭输出流
  14. Toast.makeText(this, "数据保存到"+filename+".txt"+"文件中了", Toast.LENGTH_SHORT).show();
  15. }else {
  16. Toast.makeText(this, "未找到SD卡", Toast.LENGTH_SHORT).show();
  17. }
  18. }catch(Exception e){
  19. e.printStackTrace();
  20. }
  21. }

解析部分:

同样要先判断SD卡是否存在,是否具有读写权限,再判断是否存在该文件,按行读取文件并解析,自加得出结果。

  1. //解析字符数,单词数,行数,空格数
  2. private void analysis() {
  3. String str="";
  4.  
  5. int words = 0;//单词数
  6. int chars = 0;//字符数
  7. int lines = 0;//行数
  8. int spaces=0;//空格数
  9. int marks=0;//标点符号数
  10. int character=0;//字母数
  11.  
  12. String filename=et_name.getText().toString();
  13. FileInputStream fis=null;
  14. BufferedReader br=null;
  15. try {
  16. //判断SD卡是否存在,并且是否具有读写权限
  17. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  18. File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/"+filename+".txt");
  19. if (file.exists()){//判断文件是否存在
  20. //打开文件输入流
  21. fis=new FileInputStream(file);
  22. //字符流写入了缓冲区
  23. br=new BufferedReader(new InputStreamReader(fis));
  24.  
  25. while((str=br.readLine())!=null){//readLine()每次读取一行,转化为字符串,br.readLine()为null时,不执行
  26.  
  27. char[] b=str.toCharArray();//将字符串对象中的字符转换为一个字符数组
  28. for (int i = 0; i < str.length(); i++) {
  29. if (b[i]==' '){//如果字符数组中包含空格,spaces自加1
  30. spaces++;//空格数
  31. }else if (b[i]==','||b[i]=='.'){
  32. marks++;
  33.  
  34. }
  35. }
  36.  
  37. //单词数,split()方法,返回是一个数组,根据(空格,标点符号)分割成字符串数组,数组长度就是单词长度。
  38. words+=str.split("[ \\.,]").length;//使用正则表达式实现多个分隔符进行分隔的效果。
  39.  
  40. chars+=str.length();//字符串的长度,即字符数,包括英文字母数+空格数+标点数
  41. lines++;//行数(由于每次读取一行,行数自加即可)
  42. }
  43. character=chars-(spaces+marks);//字母数=字符数-空格数-标点数
  44. //关闭文件
  45. br.close();
  46.  
  47. tv_read.setText("单词数:"+words+",字符数:"+chars+",行数:"+lines+",字母数:"+character+",空格数:"+spaces+",标点符号数:"+marks);
  48. }
  49. else {
  50. Toast.makeText(this, "不存在该文件", Toast.LENGTH_SHORT).show();
  51. }
  52. }
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }

最后看看运算结果:

全文代码:

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.  
  3. private EditText et_name;
  4. private EditText et_content;
  5. private Button btn_write;
  6. private Button btn_analysis;
  7. private TextView tv_read;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. //初始化控件
  14. initView();
  15. }
  16.  
  17. private void initView() {
  18. et_name = (EditText) findViewById(R.id.et_name);
  19. et_content = (EditText) findViewById(R.id.et_content);
  20. btn_write = (Button) findViewById(R.id.btn_write);
  21. btn_analysis = (Button) findViewById(R.id.btn_analysis);
  22. tv_read = (TextView) findViewById(R.id.tv_read);
  23.  
  24. btn_write.setOnClickListener(this);
  25. btn_analysis.setOnClickListener(this);
  26. }
  27.  
  28. //点击事件
  29. @Override
  30. public void onClick(View v) {
  31. switch (v.getId()) {
  32. case R.id.btn_write:
  33. write();
  34. btn_analysis.setClickable(true);
  35. break;
  36. case R.id.btn_analysis:
  37. analysis();
  38. break;
  39. }
  40. }
  41.  
  42. //创建文件,保存输入内容。
  43. private void write() {
  44. String filename=et_name.getText().toString();
  45. String filecontent=et_content.getText().toString();
  46. try {
  47. if (Environment.getExternalStorageState().equals
  48. (Environment.MEDIA_MOUNTED)) {//表明对象是否存在并具有读、写权限
  49. //返回 File ,获取外部存储目录即 SDCard
  50. File sdCardDir = Environment.getExternalStorageDirectory();
  51. FileOutputStream fos = new FileOutputStream(sdCardDir.getCanonicalPath()
  52. + "/"+filename+".txt");//getCanonicalPath()返回的是规范化的绝对路径
  53. fos.write(filecontent.getBytes("UTF-8"));
  54. fos.close();//关闭输出流
  55. Toast.makeText(this, "数据保存到"+filename+".txt"+"文件中了", Toast.LENGTH_SHORT).show();
  56. }else {
  57. Toast.makeText(this, "未找到SD卡", Toast.LENGTH_SHORT).show();
  58. }
  59. }catch(Exception e){
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. //解析字符数,单词数,行数,空格数
  65. private void analysis() {
  66. String str="";
  67.  
  68. int words = 0;//单词数
  69. int chars = 0;//字符数
  70. int lines = 0;//行数
  71. int spaces=0;//空格数
  72. int marks=0;//标点符号数
  73. int character=0;//字母数
  74.  
  75. String filename=et_name.getText().toString();
  76. FileInputStream fis=null;
  77. BufferedReader br=null;
  78. try {
  79. //判断SD卡是否存在,并且是否具有读写权限
  80. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  81. File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/"+filename+".txt");
  82. if (file.exists()){//判断文件是否存在
  83. //打开文件输入流
  84. fis=new FileInputStream(file);
  85. //字符流写入了缓冲区
  86. br=new BufferedReader(new InputStreamReader(fis));
  87.  
  88. while((str=br.readLine())!=null){//readLine()每次读取一行,转化为字符串,br.readLine()为null时,不执行
  89.  
  90. char[] b=str.toCharArray();//将字符串对象中的字符转换为一个字符数组
  91. for (int i = 0; i < str.length(); i++) {
  92. if (b[i]==' '){//如果字符数组中包含空格,spaces自加1
  93. spaces++;//空格数
  94. }else if (b[i]==','||b[i]=='.'){
  95. marks++;
  96.  
  97. }
  98. }
  99.  
  100. //单词数,split()方法,返回是一个数组,根据(空格,标点符号)分割成字符串数组,数组长度就是单词长度。
  101. words+=str.split("[ \\.,]").length;//使用正则表达式实现多个分隔符进行分隔的效果。
  102.  
  103. chars+=str.length();//字符串的长度,即字符数,包括英文字母数+空格数+标点数
  104. lines++;//行数(由于每次读取一行,行数自加即可)
  105. }
  106. character=chars-(spaces+marks);//字母数=字符数-空格数-标点数
  107. //关闭文件
  108. br.close();
  109.  
  110. tv_read.setText("单词数:"+words+",字符数:"+chars+",行数:"+lines+",字母数:"+character+",空格数:"+spaces+",标点符号数:"+marks);
  111. }
  112. else {
  113. Toast.makeText(this, "不存在该文件", Toast.LENGTH_SHORT).show();
  114. }
  115. }
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. }

知识浅薄,如有错误,还望指出。

个人项目耗时记录

Android 简单统计文本文件字符数、单词数、行数Demo的更多相关文章

  1. 统计文件夹下java代码行数的小程序--主要是学习任务队列的思想

    首先感谢czbk的老师,录制的视频,让我们有这么好的学习资料.……—— 统计文件夹java文件的行数,首先想到的肯定是用递归的方法,因为文件夹下面可能包含文件夹,用递归的方法,代码容易写.(这和写简单 ...

  2. 使用Eclipse可以方便的统计工程或文件的代码行数,

    使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...

  3. iOS 统计Xcode整个工程的代码行数

    小技巧5-iOS 统计Xcode整个工程的代码行数 1.打开终端 2.cd 空格 将工程的文件夹拖到终端上,回车,此时进入到工程的路径 此时已经进入到工程文件夹下 3.运行指令 a. find . - ...

  4. 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。

    https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表 ...

  5. 【原】Mac下统计任意文件夹中代码行数的工

    [链接][原]Mac下统计任意文件夹中代码行数的工http://www.cnblogs.com/wengzilin/p/4580646.html

  6. 统计sql server 2012表的行数

    --功能:统计sql server 2012表的行数 SELECT a.name, a.object_id, b.rows, b.index_id FROM sys.tables AS a INNER ...

  7. python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)

    1.代码的运行结果: 获取 指定文件夹下.指定文件格式 文件的: 总代码行数.总注释行数(需指定注释格式).总空行数: #coding: utf-8 import os, re # 代码所在目录 FI ...

  8. c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.

    #include <stdio.h> #include <ctype.h> using namespace std; /* 题目:输入一行字符,分别统计出其中英文字母.空格.数 ...

  9. C++统计代码注释行数 & 有效代码行数 & 代码注释公共行 & 函数个数

    问题来源,在14年的暑假的一次小项目当中遇到了一个这样的问题,要求统计C++代码的注释行数,有效代码行数,代码注释公共行数,以及函数个数. 下面稍微解释一下问题, 1)注释行数:指有注释的行,包括有代 ...

随机推荐

  1. spring源码 — 四、MVC

    spring mvc是怎么实现的?为什么我们只需要在方法上写一个注解,就可以通过http访问这个接口?下面我们分3部分来解答这两个问题 注意:本文是基于spring4.3.2的 spring mvc整 ...

  2. <转载>Android性能优化之HashMap,ArrayMap和SparseArray

    本篇博客来自于转载,打开原文地址已经失效,在此就不贴出原文地址了,如原作者看到请私信我可用地址,保护原创,人人有责.   Android开发者都知道Lint在我们使用HashMap的时候会给出警告—— ...

  3. Github项目推荐-图神经网络(GNN)相关资源大列表

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 转自 | AI研习社 作者|Zonghan Wu 这是一个与图神经网络相关的资源集合.相关资源浏览下方 ...

  4. 了解git的命令行使用

    git现在已经是非常大众的版本管理工具了,如果在windows下用vs这种ide,已经可以很简单的点点鼠标就完成大部分工作了. 但是在某些特殊情况用命令行时,还是需要了解很多命令的. 安装 linux ...

  5. docker 发布到私有docker registry

    1.使用vs发布项目到文件夹: 2.在文件夹中新建dockerfile文件, 内容: FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-sli ...

  6. 开发vue但不使用vue-cli和webpack相关注意事项

    1.绑定vue组件使用new Vue() 2.new Vue()需要在dom结构生成之后才有效(毕竟有需要el) 3.Vue.component注册全局组件在vue容器组件挂载之前才有效 4.当然,可 ...

  7. 还原堆栈信息,分析地形系统使用ASTC格式的纹理导致Crash的问题

    0x00 前言 在这篇文章中,我们选择了过去一周Unity官方社区交流群中比较有代表性的几个问题,总结在这里和大家进行分享.主要涵盖了IL2CPP.Scripting.Virtual Reality. ...

  8. MySQL 查询出的时间相差几个小时

    最近做的一个springboot2.0项目. 前提是我的服务器时区没有问题: [root@wangbo ~]# date -R Mon, 22 Apr 2019 19:24:33 +0800 可以参考 ...

  9. python获取set-cookies

    python获取set-cookies #!/usr/bin/python3.4 # -*- coding: utf-8 -*- import requests url = "https:/ ...

  10. Java的数组,栈,队列

    import java.util.Arrays; public class Array<E> { private E[] data; private int size; //构造函数,传入 ...