Android--查找程序根目录下所有文件/Java IO操作
来源:互联网 发布:若风淘宝 编辑:程序博客网 时间:2024/06/08 15:31
本实例实现的是浏览根目录下所有的文件,包含子目录下所有的文件,类似于实现“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. */ @Override protected 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被按下时要做的动作 */ @Override protected 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操作
- 查找程序根目录下所有文件(Java IO与ListActivity的结合)
- Android学习之查找程序根目录下的所有文件
- 查找程序根目录下所有文件-Java i/o与ListActivity
- 【Android小范例教程】【1】查找程序根目录下的文件
- 利用IO的知识,实现一个程序列出某个目录下的所有的”.java”文件。
- linux下c语言实现搜索根目录下所有文件
- Java基础--IO-列出目录下所有文件
- 编译libfdk-aac 库,使用根目录下的android.mk直接包含所有子目录下的android.mk文件
- java代码遍历目录下的所有文件(查找对应目录下的所有文件)
- 列出C盘根目录下符合规则的所有文件
- java IO 文件操作
- 【Java IO】文件操作
- java文件IO操作
- java文件IO操作
- java 文件操作 Io
- JAVA文件IO操作
- java IO 文件操作
- android_分享 图片 和 文字
- Working with user roles and permissions in SharePoint Object Model
- 关于学习新技术的方法和让自己不断进步的一点建议
- 在HBase上查询地理信息系统(HBase in Action)
- Json数据异步绑定到界面的Table并且自动刷新
- Android--查找程序根目录下所有文件/Java IO操作
- 接口与抽象类的区别
- PAT 1025. PAT Ranking (25)
- java程序运行时出现问题:Could not find the main class: test_jena. Program will exit.
- 位图
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- 关于wchar的两个常用函数wcstombs和WideCharToMultiByte
- 深入浅出 JQuery (一) 浅析JQuery
- 第三章 环境变量