zoukankan      html  css  js  c++  java
  • 字符数 单词数 行数的计算

    首先画一个界面:

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.04"
            android:text="文件内容:" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.04"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.03"
            android:text="解析" />
    

    代码部分:

     editText = (EditText)findViewById(R.id.editText);
            button = (Button)findViewById(R.id.button);
            textView = (TextView)findViewById(R.id.textView);
    
     public void onClick(View v) {
                    int charnumber = 0 ; //字符
                    int words = 0; //单词
                    int linenumber = 0; //行数
                    String filename=editText.getText().toString();
                    try {
                        File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/"+filename+".txt");
                        FileInputStream isr=new FileInputStream(file);
                        BufferedReader br = new BufferedReader(new InputStreamReader(isr));
    
                        while( br.read()!= -1){
                            String s = br.readLine();
                            charnumber+=s.length();
                            words +=s.split(" ").length;
                            linenumber ++;
                        }
                        isr.close();
                        textView.setText("字符数:"+charnumber+"	单词数:"+words+"行 数:"+linenumber);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    

    运算界面如下:

  • 相关阅读:
    php内核为变量的值分配内存的几个宏
    php7 引用成为一种类型
    function参数
    execvp php-fpm reload使用的函数
    fastcgi
    php-fpm定时器
    php 类继承
    php 对象 调用静态方法
    php unset变量
    php5数组与php7数组区别
  • 原文地址:https://www.cnblogs.com/liqinsqzr/p/6629101.html
Copyright © 2011-2022 走看看