WC项目解析统计文本文件中的字符数、行数、单词数

用androidstudio解析统计文本文件中的字符数、单词数、行数,用editText显示要解析的文本文档。
首先是他的界面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<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/edt_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_show"
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_jiexi"
android:textSize="20dp"/>

</LinearLayout>

然后我们需要把我们所需要解析的文件来保存在手机上,然后通过按钮监控来进行解析。

首先定义属性:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_content;
    private Button btn_show;
    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_content = (EditText) findViewById(R.id.et_content);
        btn_show = (Button) findViewById(R.id.btn_show);
        btn_analysis = (Button) findViewById(R.id.btn_analysis);
        tv_read = (TextView) findViewById(R.id.tv_read);

        btn_show.setOnClickListener(this);
        btn_analysis.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_show:
                show();
                break;
            case R.id.btn_analysis:
                analysis();
                break;
        }
    }

接下来保存文件:

 //保存文件
    private void write() {
        String s = et_content.getText().toString();
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File sdDire = Environment.getExternalStorageDirectory();
                FileOutputStream outFileStream = new FileOutputStream(sdDire.getCanonicalPath() + "/test.txt");
                outFileStream.write(s.getBytes());
                outFileStream.close();
                Toast.makeText(this, "数据保存到text.txt文件了", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "未找到内存卡", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

接下来解析文本文档
//统计文本
    private void analysis() {
        String st = "";
        int words=0;//单词数
        int chars=0;//字符数
        int lines=0;//行数
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/test.txt");
                    // 打开文件输入流
                    FileInputStream fir = new FileInputStream(file);
                    BufferedReader reads = new BufferedReader(new InputStreamReader(fir));
                    while ((st = reads.readLine()) != null) {
                        words+=st.split("[ \\.,?]").length;//用于把一个字符串分割成字符串数组,字符串数组的长度,就是单词个数
                        chars+=st.length();//字符串的长度,即字符数
                        lines++;//行数(由于每次读取一行,行数自加即可)
                    }
                    fir.close();
                    tv_read.setText("单词数:"+words+",字符数:"+chars+",行数:"+lines);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

得到的结果是这样

这次的个人耗时对比记录表:

 


 

posted @ 2017-03-26 23:18  小绒花  阅读(800)  评论(0编辑  收藏  举报