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. AddForce给物体添加刚体效果并且脚本增加一个力(按空格实现)

    using UnityEngine; using System.Collections; public class CubeAddForce : MonoBehaviour { float hor,v ...

  2. 【C#基础】byte二进制数组转string

    //解析post请求数组返回的数组 //解码返回的二进制数组 public string DecodeBytes(byte[] c) { string html = string.Empty; try ...

  3. HTML, CSS学习笔记(完整版)

    第一章 div布局 前几课内容 .htm是早期的后缀.由于那时仅仅能支持长度为3的后缀.因此html与htm是一样的. shtml是server先处理然后再交给浏览器处理 #HTML小知识#之#XHT ...

  4. jquery——zTree, 完美好用的树插件

    Demo 这绝对是我见过最完美的tree了,尽管是国产货,但一点不输国外产品,国外的还没有见过这么强的. _______________________________________________ ...

  5. linux程序自启动和新建linux服务的方法

    1 linux创建自启动程序    自启动的两种方法,都经过自己测试.1.1 自启动程序方法1:    在etc/rc.local在里面加入/home/robin/code/autoruntest & ...

  6. [Linux 小技巧] Ubuntu 14.04 下编译、安装、配置最新开发版 GoldenDict

    1. 背景介绍 GoldenDict 是一款非常优秀的跨平台电子词典软件,支持 StarDict.Babylon 等多种词典.其 PC 版基于 Qt 编写,完全免费.开源且没有广告.GoldenDic ...

  7. Unity用户自定义圆角头像

    前天朋友遇到一个这样的需求,而且比较棘手让我帮忙解决.需求就是棋牌类的游戏,玩家的个人资料中包括自己的头像而且可以浏览相册中的图片或者使用相机拍照设置.关于这个问题我也查阅一些资料,由于涉及安卓部分知 ...

  8. 【转】UIKit性能调优实战讲解

    文/bestswifter(简书作者)原文链接:http://www.jianshu.com/p/619cf14640f3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 在使用UIKi ...

  9. ASP.NET 获取IP信息等探针

    获取客户端:HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]: 获取URL Request.RawUrl:获取客 ...

  10. Lambda表达式 - 浅谈

    概述: 只要有委托参数类型的地方,就可以使用 Lambda表达式.在讲述Lambda表达式之前,有必要先简要说明一下 委托中的"匿名方法": using System; using ...