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. Rational AppScan 扫描大型网站

    Rational AppScan 工作原理 Rational AppScan(简称 AppScan)其实是一个产品家族,包括众多的应用安全扫描产品,从开发阶段的源代码扫描的 AppScan sourc ...

  2. C#调用R语言输出图片

    参考:http://rdotnet.codeplex.com/documentation   REngine.SetEnvironmentVariables(); REngine engine = R ...

  3. 数据视化Echarts+百度地图API实现市县区级下钻

    开始 这两天公司有个页面需要做数据可视化的展示,数据视化采用的是Echarts+百度地图API做展示,需要用到县级区级下钻的一个联动效果发现网上关于Echarts做到县区级下钻的资料很少,有的话也不是 ...

  4. Stack and queue.

    队列的定义及基本运算 1.定义    队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表 (1)允许删除的一端称为队头(Front). (2)允许插入的一端称为队尾(Rea ...

  5. Facebook Architecture

    Facebook Architecture Quora article a relatively old presentation on facebook architecture another I ...

  6. OpenXML_导入Excel到数据库

    (1).实现功能:通过前台选择.xlsx文件的Excel,将其文件转化为DataTable和List集合 (2).开发环境:Window7旗舰版+vs2013+Mvc4.0 (2).在使用中需要用到的 ...

  7. oracle 优化——索引与组合索引

    1.索引结构.第一张图是索引的官方图解,右侧是存储方式的图解. 图中很清晰的展示了索引存储的状况. 在leaf 节点中存储了一列,索引所对应项的 :值,rowId,长度,头信息(控制信息) 这样我们就 ...

  8. SwfUpload及imgareaselect使用方法

    1.导入文件 Swfupload相关文件 imgareaselect截取插件相关文件 2.前端html代码 添加一个截取图片的按钮,其他为swf所需的html. <body> <di ...

  9. DOM 之Range(范围)

    -------<javascript高级程序设计>  12.4 范围  笔记------- DOM2级在Document类型中定义了createRange()方法,在兼容DOM的浏览器中, ...

  10. 微信支付:redirect_uri参数错误 的解决办法

    redirect_url参数错误: 报这个错误,说明你的公众号后台授权设置有问题(一般有两处) 一:检查授权目录 答:支付授权目录是网站发起请求的页面所在目录,并且必须是能通过url地址访问的(与真实 ...