做的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 FileOutputStreamsdCardDir.getCanonicalPath) + "/"+filename+".txt");//getCanonicalPath)返回的是规范化的绝对路径 fos.writefilecontent.getBytes"UTF-8")); fos.close);//关闭输出流 Toast.makeTextthis, "数据保存到"+filename+".txt"+"文件中了", Toast.LENGTH_SHORT).show); }else { Toast.makeTextthis, "未找到SD卡", Toast.LENGTH_SHORT).show); } }catchException 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).equalsEnvironment.MEDIA_MOUNTED)) { File file = new FileEnvironment.getExternalStorageDirectory).getCanonicalPath) + "/"+filename+".txt"); if file.exists)){//判断文件是否存在 //打开文件输入流 fis=new FileInputStreamfile); //字符流写入了缓冲区 br=new BufferedReadernew InputStreamReaderfis)); whilestr=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.makeTextthis, "不存在该文件", 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 onCreateBundle savedInstanceState) { super.onCreatesavedInstanceState); setContentViewR.layout.activity_main); //初始化控件 initView); } private void initView) { et_name = EditText) findViewByIdR.id.et_name); et_content = EditText) findViewByIdR.id.et_content); btn_write = Button) findViewByIdR.id.btn_write); btn_analysis = Button) findViewByIdR.id.btn_analysis); tv_read = TextView) findViewByIdR.id.tv_read); btn_write.setOnClickListenerthis); btn_analysis.setOnClickListenerthis); } //点击事件 @Override public void onClickView v) { switch v.getId)) { case R.id.btn_write: write); btn_analysis.setClickabletrue); 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 FileOutputStreamsdCardDir.getCanonicalPath) + "/"+filename+".txt");//getCanonicalPath)返回的是规范化的绝对路径 fos.writefilecontent.getBytes"UTF-8")); fos.close);//关闭输出流 Toast.makeTextthis, "数据保存到"+filename+".txt"+"文件中了", Toast.LENGTH_SHORT).show); }else { Toast.makeTextthis, "未找到SD卡", Toast.LENGTH_SHORT).show); } }catchException 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).equalsEnvironment.MEDIA_MOUNTED)) { File file = new FileEnvironment.getExternalStorageDirectory).getCanonicalPath) + "/"+filename+".txt"); if file.exists)){//判断文件是否存在 //打开文件输入流 fis=new FileInputStreamfile); //字符流写入了缓冲区 br=new BufferedReadernew InputStreamReaderfis)); whilestr=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.makeTextthis, "不存在该文件", Toast.LENGTH_SHORT).show); } } } catch Exception e) { e.printStackTrace); } } }
知识浅薄,如有错误,还望指出。
个人项目耗时记录


