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 ...
随机推荐
- php form表单post提交获取不到数据,而使用get提交能获取到数据 的解决办法
开发环境:xampp,mac,phpstorm 其实出现这个问题的原因就是在于phpstorm,它默认使用的是自带的内部服务器,这个服务器使用63342端口,而且服务器内部有问题,导致POST方法异常 ...
- IT English Collection(16) of Message
1 前言 本文介绍了关于Objective-C中的消息机制,详情如下. 转载请注明出处:http://blog.csdn.net/developer_zhang 2 详述 2.1 原文 A messa ...
- BNU10804:域名统计
域名(Domain Name),是由一串用点分隔的名字组成的Internet上某一台计算机或计算机组的名称,用于在数据传输时标识计算机的电子方位(有时也指地理位置),目前域名已经成为 互联网的品牌.网 ...
- apache ab工具对网站进行压力测试
Apache -- ab工具主要测试网站的(并发性能) 这个工具非常的强大. 基本语法 : cmd>ab.exe –n 请求总次数 -c 并发数 请求页面的url 进入到ab.ex ...
- C# winCE5.0开发右键效果解决方案
用VS2008开发C#语言wince程序,发现程序里右键捕获不到,采集器上点也没反应,上网查好像有个c++版本的,看不懂啊,下面我给出C#实现右键效果的解决方案,请各位多多优化. 首先控件Contex ...
- (二)《Java编程思想》——t h i s 关键字
this 关键字(注意只能在方法内部使用)可为已调用了其方法的那个对象生成相应的句柄.可象对待其他任何对象句柄一样对待这个句柄. package chapter4; //: Leaf.java // ...
- JavaScript中函数参数的按值传递与按引用传递(即按地址传递)
首先声明一句:JavaScript中所有函数的参数都是按值传递的!不存在按引用传递! 在讲传递参数之前我们先来讲一下指针. 学过C指针的应该都知道,指针变量中保存的是一个地址,程序可以根据所保存的地址 ...
- RecycleView 瀑布流滑动移位
RecycleView StaggeredLayoutManager(瀑布流)滑动的时候,默认会出现item移动的问题,需以下来个步骤来解决: 附上StaggeredLayoutManager中的一段 ...
- XCode请求数据中接收类型的后台与前台处理(本机模拟)
Xcode报错问题如下: 解决办法如下: 0x1 ->请求数据时加上缺少的类型 AFHTTPSessionManager *manager = [selfAFHTTPSessionMan ...
- 高仿QQ即时聊天软件开发系列之三登录窗口用户选择下拉框
上一篇高仿QQ即时聊天软件开发系列之二登录窗口界面写了一个大概的布局和原理 这一篇详细说下拉框的实现原理 先上最终效果图 一开始其实只是想给下拉框加一个placeholder效果,让下拉框在未选择未输 ...