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();
                    }
                }
    

    运算界面如下:

  • 相关阅读:
    解决IE6浏览器下position:fixed固定定位问题
    CSS中overflow:hidden在ie6、ie7无效不能隐藏解决办法
    liunx 中删除export设置的环境变量
    文件操作
    集合操作
    三级菜单
    字典操作
    字符串操作
    购物车程序
    列表操作
  • 原文地址:https://www.cnblogs.com/liqinsqzr/p/6629101.html
Copyright © 2011-2022 走看看