• Android文档次数统计


    统计文档字符数,行数,单词数。从其他地方加载一个文件来检索文档字数我不会 0.0 所以做了一个输入文档并保存然后再检索字数,行数。

    布局设计

    `

    <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/text"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="内容:"
        android:textSize="15dp"/>
    <EditText
        android:inputType="textMultiLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/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/save"
            android:text="保存"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/answer"
            android:text="解析"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/read"
        android:textSize="20dp"/>
    

    我们需要把文件保存到手机上所以要在AndroidMainfest.xml中添加权限

    保存文件

    `private void write() {
        String filename=text.getText().toString();
        String filecontent=content.getText().toString();
        try {
            if (Environment.getExternalStorageState().equals
                    (Environment.MEDIA_MOUNTED)) { 
                File sdCardDir = Environment.getExternalStorageDirectory();
                FileOutputStream fos = new FileOutputStream(sdCardDir.getCanonicalPath()+ "/"+filename+".txt");
                fos.write(filecontent.getBytes("UTF-8"));
                fos.close();/
                Toast.makeText(this, "文件保存到了"+filename+".txt"+"中", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "未找到文件", Toast.LENGTH_SHORT).show();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    

    解析文件

    `private void analysis() {
        String str="";
        int lines = 0;//行数
        int words = 0;//单词数
        int chars = 0;//字符数
        String filename=et_name.getText().toString();
        FileInputStream fis=null;
        BufferedReader br=null;
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/"+filename+".txt");
                if (file.exists()){                    
                    fis=new FileInputStream(file);
                    br=new BufferedReader(new InputStreamReader(fis));
                    while((str=br.readLine())!=null){
                        char[] b=str.toCharArray();
                        for (int i = 0; i < str.length(); i++) {
                            if (b[i]==' '){
                                spaces++;
                            }else if (b[i]==','||b[i]=='.'){
                                marks++;
                            }
                        }
                    }
                    character=chars-(spaces+marks);
                    br.close();
                    read.setText("单词数:"+words+",字符数:"+chars+",行数:"+lines+");
                }
                else {
                    Toast.makeText(this, "未找到文件", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    软件结果

    个人软件时间

    未经本人同意禁止转载!!!!!

  • 相关阅读:
    052-34
    052-33
    052-32
    052-31
    052-30
    052-28
    react组件之间通信
    排序算法
    点外卖
    js的各种排序算法
  • 原文地址:https://www.cnblogs.com/Higanbana/p/6623817.html
Copyright © 2020-2023  润新知