本实例实现的是浏览根目录下所有的文件,包含子目录下所有的文件,类似于实现“Windows资源管理器”,首先,先看下程序的实现截图:

这个程序应用的是ListActivity和JavaIO的知识。

首先,让我们先看下主程序的布局文件定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"
><TextView android:id="@+id/mPath"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="5px"android:textSize="18sp"android:textColor="@drawable/blue"/><ListView android:id="@android:id/list"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
</LinearLayout>

其中,ListView中的各个子TextView也是由程序提供,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"
/>

主程序的实现代码如下:

public class EX04_21 extends ListActivity
{/* 对象声明 items:存放显示的名称paths:存放文件路径rootPath:起始目录*/private List<String> items=null;private List<String> paths=null;private String rootPath="/";private TextView mPath;/** Called when the activity is first created. */@Overrideprotected void onCreate(Bundle icicle){super.onCreate(icicle);/* 加载main.xml Layout */setContentView(R.layout.main);mPath=(TextView)findViewById(R.id.mPath);getFileDir(rootPath);}/* 取得文件架构的method */private void getFileDir(String filePath){/* 设定目前所在路径 */mPath.setText(filePath);items=new ArrayList<String>();paths=new ArrayList<String>();  File f=new File(filePath);  File[] files=f.listFiles();if(!filePath.equals(rootPath)){/* 第一笔设定为[回到根目录] */items.add("Back to "+rootPath);paths.add(rootPath);/* 第二笔设定为[回上层] */items.add("Back to ../");paths.add(f.getParent());}/* 将所有文件加入ArrayList中 */for(int i=0;i<files.length;i++){File file=files[i];items.add(file.getName());paths.add(file.getPath());}/* 声明一ArrayAdapter,使用file_row这个Layout,并将Adapter设定给此ListActivity */ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,R.layout.file_row, items);setListAdapter(fileList);}/* 设定ListItem被按下时要做的动作 */@Overrideprotected void onListItemClick(ListView l,View v,int position,long id){File file = new File(paths.get(position));if(file.canRead()){if (file.isDirectory()){/* 如果是文件夹就再进去读取 */getFileDir(paths.get(position));}else{/* 如果是文件,则弹出AlertDialog */new AlertDialog.Builder(this).setTitle("Message").setMessage("["+file.getName()+"] is File!").setPositiveButton("OK",new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int which){}}).show();         }}else{/* 弹出AlertDialog显示权限不足 */new AlertDialog.Builder(this).setTitle("Message").setMessage("权限不足!").setPositiveButton("OK",new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int which){}}).show();     }}
}

Android--查找程序根目录下所有文件/Java IO操作相关推荐

  1. 忙里偷闲写的小例子---读取android根目录下的文件或文件夹

    From:http://www.cnblogs.com/wenjiang/p/3140055.html 最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还 ...

  2. 【已解决】请在位于当前 Web 应用程序根目录下的“web.config”配置文件中创建一个 <customErrors> 标记

    问题 详细信息: 若要使他人能够在远程计算机上查看此特定错误信息的详细信息,请在位于当前 Web 应用程序根目录下的"web.config"配置文件中创建一个 标记.然后应将此 标 ...

  3. Linux(/)根目录下各个文件夹的含义及使用

    Linux根目录下的文件夹 1./- 根 每一个文件和目录都是从根目录开始. 只有root用户可在该目录下的进行写操作(拥有写权限).注意:/root是root用户的主目录,这与/.不一样 /root ...

  4. linux如何查看指定目录下文件内容,Linux 系统下通过关键词查找指定目录下的文件内容...

    #!/bin/bash # 作者:靑龍一笑(C.S.Ricen) # 功能:根据指定的关键词,查找指定目录下的文件内容 # 要查找的目录 Search_Dir=/opt/datas/ # 关键字列表 ...

  5. proc文件系统探索 之 根目录下的文件[三]

    包括对proc根目录下meminfo文件的解析. > cat /proc/meminfo   读出的内核信息进行解释, 下篇文章会简单对读出该信息的代码进行简单的分析. MemTotal: 50 ...

  6. linux 查看整个根目录下各个文件占用情况

    su root 切换到root用户下,输入密码即可切换到当前用户下 df -h 命令查看磁盘空间 du -ah --max-depth=1  /    查看根目录下各个文件占用情况,max-depth ...

  7. 如何在ftp服务器下查找文件夹,查找ftp服务器下的文件夹名

    查找ftp服务器下的文件夹名 内容精选 换一换 Linux x86-64(64位)服务器,常见的有EulerOS.Ubuntu.Debian.CentOS.OpenSUSE等.Windows 7及以上 ...

  8. Steam根目录下userdata文件夹命名规则

    Steam根目录下"userdata"文件夹命名规则 userdata的初文件夹命名方式 steamID分类 steam16位ID换算32 steam64位ID换算32 userd ...

  9. LoadRunner 是要保存此文件,还是要联机查找程序来打开此文件

    软件 1.LoadRunner:LoadRunner12.0.2 LoadRunner12.55(两个版本我都用过,都会出现这种情况) 2.浏览器:IE11 火狐29(两个浏览器都试过,记得要用Loa ...

最新文章

  1. mysql 存储guid_我应该如何在MySQL表中存储GUID?
  2. 实训项目1-熟练使用VMware安装Windows server 2012
  3. Linux文件被自动加属性保护,Linux下如何对文件进行权限保护以防止文件被人改动...
  4. cocos2dx 3.2之Lua打飞机项目
  5. 服务器输入字符在客户端显示,管理 Unicode 服务器与非 Unicode 客户端之间的数据转换...
  6. Java 8 - Interface Default Method接口默认方法
  7. python装饰器解析_Python 装饰器解析
  8. 【转】Win2008 r2 远程桌面授权已过期的解决办法
  9. 如何查看linux CPU总占用率?
  10. 拓端tecdat|通过Python中的Apriori算法进行关联规则挖掘
  11. python lasso回归分析_解析python实现Lasso回归
  12. 【老生谈算法】matlab实现LEACH 算法——LEACH 算法
  13. UART协议TTL电平接口高性能低功耗SI4463无线透传通信模块
  14. 多元化邮件插图成鲜活生命力,助力邮件营销转化!
  15. 用*号输出字母C的图案。
  16. Geek 设计师们疯狂的桌面
  17. html转换成word文档没有边框,解决 apache poi 转换 word(docx) 文件到 html 文件表格没边框的问题...
  18. 使用vimdiff做git的diff与merge工具
  19. 离群点(孤立点、异常值)检测方法
  20. html css精灵,CSS spirit /css精灵

热门文章

  1. Html内容超出标记宽度后自动隐藏
  2. Java计算两个时间差
  3. [bat] 使用bat文件保证指定程序运行
  4. Stream流与Lambda表达式(四) 自定义收集器
  5. 香港居民换领新智能身份证 市民对办理过程表满意
  6. 约瑟夫环(约瑟夫问题)求最后出列的人数
  7. 赛门铁克:“高度怀疑”WannaCry的幕后黑手是朝鲜
  8. MySQL INNODB Plugin 测试(二)
  9. object-c 常见问题
  10. java常见问题排查