字符 单词数 行数

布局代码:

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/et_writ"

android:singleLine="false"

android:hint="请输入数据"

android:imeOptions="actionDone"

/>// 监听回车键

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/btn"

android:text="开始解析数据"

android:textSize="20dp"

/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"> <TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/tv_v" android:text="显示数据"

/>

核心功能代码

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

}

}

原文地址:https://www.cnblogs.com/chenbaiwan/p/6629427.html