1. activity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件名" />
<EditText
android:hint="foldername"
android:id="@+id/ET_Folder"
android:layout_width="140dip"
android:layout_height="wrap_content" />
<Button
android:text="打开"
android:id="@+id/But_Open"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="清除"
android:id="@+id/But_Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> <EditText
android1:id="@+id/ET_FileName"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:ems="10" >
</EditText> <ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:editable="false"
android:id="@+id/ET_FileContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView> </LinearLayout>

2. MainActivity.java文件

/*读取输入的某个文件夹中所有的txt文件
* 显示文件名、文件内容
* */ import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText et_folder; //输入的文件夹名
private Button bt_open; //打开按钮
private Button bt_clear; //清除按钮
private EditText et_filename; //用于显示文件名
private EditText et_filecontent; //用于显示txt文件内容 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_folder = (EditText) findViewById(R.id.ET_Folder);
et_filename = (EditText) findViewById(R.id.ET_FileName);
et_filecontent = (EditText) findViewById(R.id.ET_FileContent); bt_open = (Button) findViewById(R.id.But_Open);
bt_open.setOnClickListener(new OnClickListener(){//打开按钮监听
public void onClick(View arg0) {
//若输入的文件夹名为空
if(et_folder.getText().toString().trim().equals("")){
Toast.makeText(getApplicationContext(),
"输入为空",Toast.LENGTH_SHORT).show();
}else{
// 获得SD卡根目录路径 "/sdcard"
File sdDir = Environment.getExternalStorageDirectory();
File path = new File(sdDir+File.separator
+et_folder.getText().toString().trim()); // 判断SD卡是否存在,并且是否具有读写权限
if (Environment.getExternalStorageState().
equals(Environment.MEDIA_MOUNTED)) {
File[] files = path.listFiles();// 读取文件夹下文件
et_filename.setText("");
et_filecontent.setText(""); et_filename.setText(getFileName(files));
et_filecontent.setText(getFileContent(files));
}
}
}
}); bt_clear = (Button) findViewById(R.id.But_Clear);
bt_clear.setOnClickListener(new OnClickListener(){//清除按钮监听
public void onClick(View arg0) {
et_folder.setText("");
et_filename.setText("");
et_filecontent.setText("");
}
}); } //读取指定目录下的所有TXT文件的文件内容
protected String getFileContent(File[] files) {
String content = "";
if (files != null) { // 先判断目录是否为空,否则会报空指针
for (File file : files) {
//检查此路径名的文件是否是一个目录(文件夹)
if (file.isDirectory()) {
Log.i("zeng", "若是文件目录。继续读1" +
file.getName().toString()+ file.getPath().toString());
getFileContent(file.listFiles());
Log.i("zeng", "若是文件目录。继续读2" +
file.getName().toString()+ file.getPath().toString());
} else {
if (file.getName().endsWith(".txt")) {//格式为txt文件
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader =
new InputStreamReader(instream, "GBK");
BufferedReader buffreader =
new BufferedReader(inputreader);
String line="";
//分行读取
while (( line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();
}
}
catch (java.io.FileNotFoundException e) {
Log.d("TestFile", "The File doesn't not exist.");
}
catch (IOException e) {
Log.d("TestFile", e.getMessage());
} }
}
} }
return content ;
} //读取指定目录下的所有TXT文件的文件名
private String getFileName(File[] files) {
String str = "";
if (files != null) { // 先判断目录是否为空,否则会报空指针
for (File file : files) {
if (file.isDirectory()){//检查此路径名的文件是否是一个目录(文件夹)
Log.i("zeng", "若是文件目录。继续读1"
+file.getName().toString()+file.getPath().toString());
getFileName(file.listFiles());
Log.i("zeng", "若是文件目录。继续读2"
+file.getName().toString()+ file.getPath().toString());
} else {
String fileName = file.getName();
if (fileName.endsWith(".txt")) {
String s=fileName.substring(0,fileName.lastIndexOf(".")).toString();
Log.i("zeng", "文件名txt:: " + s);
str += fileName.substring(0,fileName.lastIndexOf("."))+"\n";
}
}
} }
return str;
} }

Android 读取手机某个文件夹目录及子文件夹中所有的txt文件的更多相关文章

  1. 文件夹中含有子文件夹,修改子文件夹中的图像存储格式(python实现)

    文件夹中含有子文件夹,修改子文件夹中的图像存储格式,把png图像改为jpg图像,python代码如下: import os import cv2 filePath = 'C:\\Users\\admi ...

  2. JAVA中删除文件夹下及其子文件夹下的某类文件

    ##定时删除拜访图片 ##cron表达式 秒 分 时 天 月 ? ##每月1日整点执行 CRON1=0 0 0 1 * ? scheduled.enable1=false ##图片路径 filePat ...

  3. asp.net 遍历文件夹下全部子文件夹并绑定到gridview上

    遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = n ...

  4. Matlab学习:读取指定文件夹及其五级子文件夹内的文件

    OpenCV2.4.X版本提供了三个函数来读取指定目录内的文件,它们分别是: (1)GetListFiles:读取指定目录内所有文件,不包含子目录: (2)GetListFilesR:读取指定目录及其 ...

  5. NSIS如何对一整个目录文件夹(包括子文件夹和其中的文件)压缩

    原来不加/r参数,NSIS编译器就会不认识文件夹啊. File /r [dir] Reference: http://stackoverflow.com/questions/7973242/nsis- ...

  6. Python扫描指定文件夹下(包含子文件夹)的文件

    扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...

  7. Android程序函数 将assets文件夹下的文件复制到手机的sd卡中(包括子文件夹)

    最近在做个功能是将asset文件夹下的所有文件(包括子文件)全部拷贝出来到指定目录下.所用的方法无非是用AssetManager.但是这里 有个问题是也要讲子文件夹和子文件都要拷贝出来.到网上Goog ...

  8. Linux C 读取文件夹下所有文件(包括子文件夹)的文件名【转】

    转自:https://www.cnblogs.com/xudong-bupt/p/3504442.html 本文:http://www.cnblogs.com/xudong-bupt/p/350444 ...

  9. c++读取文件夹及子文件夹数据

    这里有两种情况:读取文件夹下所有嵌套的子文件夹里的所有文件  和 读取文件夹下的指定子文件夹(或所有子文件夹里指定的文件名) <ps,里面和file文件有关的结构体类型和方法在 <io.h ...

随机推荐

  1. chrome devtools 实用快捷键

    Ctrl + O:查找资源,非常使用 Ctrl + Shift + C:切换审查元素模式与浏览器窗口模式 Ctrl + Shift + F:在源代码中搜索 Ctrl + G:跳转到指定行

  2. (转)iOS消息推送机制中pem文件的生成

    转自:http://2015.iteye.com/blog/1567777 以前写了一篇文章:iOS消息推送机制的实现,这篇文章中生成的是p12文件,但是php是用的pem文件,生成的方法和p12文件 ...

  3. [ACM] POJ 1442 Black Box (堆,优先队列)

    Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Descrip ...

  4. Linux Resin 安装配置

    Resin是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能优良,resin自身采用Java语言开发.Resin Pro版本支持缓存和负载均衡,收费最 ...

  5. Linux以KB显示内存大小

    Linux以KB显示内存大小 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free -k total used free shared buffers ca ...

  6. 如何使用easyUI

    一.简介 以下内容来自百度: jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的 目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不 ...

  7. js生成随机数的方法小结

    js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任意随机数就好理解了.比如实际中 ...

  8. IIS Express 终极玩法

    进入正题之前,先吐嘈下微软的win10升级吧.之前有意不想将win 7 旗舰版(也是破解版哈)升到win 10,自从微软推出win10后,其本上对其没多大兴趣,感觉系统各种卡顿,各种不流畅,界面各种不 ...

  9. spring 配置文件 数据库引入

    一.mysql数据库引入 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSourc ...

  10. js引用类型姿势

    栈 1)var a=new Array(),a.push(a,b,...),a.pop() queue 1)var a=new Array(), a.push(a,b,...),a.shift() a ...