Android文档统计——统计文档字符数、单词数、行数

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文件的内容:"
        android:textSize="15dp" />

    <EditText
        android:id="@+id/edt_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="保存" />

        <Button
            android:id="@+id/btn_analysis"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="解析" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_jiexi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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-27 12:25  八七七八  阅读(569)  评论(0编辑  收藏  举报