首先画一个界面:
<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+"\t单词数:"+words+"行 数:"+linenumber);
} catch (IOException e) {
e.printStackTrace();
}
}
运算界面如下:
![](https://images2015.cnblogs.com/blog/1119689/201703/1119689-20170327220627139-814495681.png)
![](https://images2015.cnblogs.com/blog/1119689/201703/1119689-20170327220658451-1479838235.png)