Android 读取手机某个文件夹目录及子文件夹中所有的txt文件
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文件的更多相关文章
- 文件夹中含有子文件夹,修改子文件夹中的图像存储格式(python实现)
文件夹中含有子文件夹,修改子文件夹中的图像存储格式,把png图像改为jpg图像,python代码如下: import os import cv2 filePath = 'C:\\Users\\admi ...
- JAVA中删除文件夹下及其子文件夹下的某类文件
##定时删除拜访图片 ##cron表达式 秒 分 时 天 月 ? ##每月1日整点执行 CRON1=0 0 0 1 * ? scheduled.enable1=false ##图片路径 filePat ...
- asp.net 遍历文件夹下全部子文件夹并绑定到gridview上
遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = n ...
- Matlab学习:读取指定文件夹及其五级子文件夹内的文件
OpenCV2.4.X版本提供了三个函数来读取指定目录内的文件,它们分别是: (1)GetListFiles:读取指定目录内所有文件,不包含子目录: (2)GetListFilesR:读取指定目录及其 ...
- NSIS如何对一整个目录文件夹(包括子文件夹和其中的文件)压缩
原来不加/r参数,NSIS编译器就会不认识文件夹啊. File /r [dir] Reference: http://stackoverflow.com/questions/7973242/nsis- ...
- Python扫描指定文件夹下(包含子文件夹)的文件
扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...
- Android程序函数 将assets文件夹下的文件复制到手机的sd卡中(包括子文件夹)
最近在做个功能是将asset文件夹下的所有文件(包括子文件)全部拷贝出来到指定目录下.所用的方法无非是用AssetManager.但是这里 有个问题是也要讲子文件夹和子文件都要拷贝出来.到网上Goog ...
- Linux C 读取文件夹下所有文件(包括子文件夹)的文件名【转】
转自:https://www.cnblogs.com/xudong-bupt/p/3504442.html 本文:http://www.cnblogs.com/xudong-bupt/p/350444 ...
- c++读取文件夹及子文件夹数据
这里有两种情况:读取文件夹下所有嵌套的子文件夹里的所有文件 和 读取文件夹下的指定子文件夹(或所有子文件夹里指定的文件名) <ps,里面和file文件有关的结构体类型和方法在 <io.h ...
随机推荐
- [转载]SQL Server查找包含某关键字的存储过程3种方法
存储过程都写在一个指定的表中了,我们只要使用like查询就可以实现查询当前这台SQL Server中所有存储过程中包括了指定关键字的存储过程并显示出来,下面一起来看看我总结了几条命令. 例子1 代码如 ...
- 10个热门IT证书
MCP (微软专家认证) CCNA (思科认证网络支持工程师) MCPD (微软认证开发专家) SCJP (SUN认证Java程序员) CISSP (信息系统安全认证专家) CompTIA A+认证 ...
- LDAP索引及缓存优化
一.设置索引 索引将查找信息和 Directory Server 条目关联起来. Directory Server支持以下几种索引: 1出现索引 (pres) - 列出了具有特定属性的条目,与属性的值 ...
- hibernate连接数据库,进行操作的步骤
//初始化 Configuration conf=null; SessionFactory sf=null; Session session=null; Transaction tx=null; tr ...
- Css的三大机制(特性):特殊性、继承、层叠详解
继承(Inheritance)是从一个元素向其后代元素传递属性值所采用的机制.确定应当向一个元素应用那些值时,用户代理(浏览器)不仅要考虑继承,还要考虑声明的特殊性(specificity),另外需要 ...
- ResourceDictionary 和 XAML 资源引用
XAML 定义应用的 UI,并且 XAML 也可以定义 XAML 中的资源.资源通常是对你希望多次使用的某些对象的定义.你要为 XAML 资源定义一个键,以供将来引用,该键的作用类似于资源的名称.你可 ...
- 使用CSS3+jquery.js 实现微信抽奖转盘效果
上次发表了一篇 微信抽奖转盘活动-效果源码分析 最近想起了刚接到这个项目时第一时间脑海里迸出的解决方法 “CSS3”! 为什么不能用CSS3来实现呢? 所以我打算用CSS3来实现这个效果.并不需要依赖 ...
- jQuery 定位锚点
// errorLine 为目标元素 scrollOffset(errorLine.offset()); function scrollOffset(scroll_offset) { $(" ...
- C++ 的多重继承
不能够从对象访问基类的公开方法,真悲剧!只能在类里面提供公共函数! void Mentor::GetInfo(){ cout<<endl<<name<<endl&l ...
- Java学习笔记--Socket和ServerSocket
参考资料: http://haohaoxuexi.iteye.com/blog/1979837http://zhidao.baidu.com/link?url=OeOSa0YbOzSbMVPa8sgP ...