android文件API使用
来源:互联网 发布:lol小兵数据 编辑:程序博客网 时间:2024/05/16 18:10
(一)获取总根
- File[] fileList=File.listRoots();
- //返回fileList.length为1
- //fileList.getAbsolutePath()为"/"
- //这就是系统的总根
(二)打开总根目录
- File file=new File("/");
- File[] fileList=file.listFiles();
- //获取的目录中除了"/sdcard"和"/system"还有"/data"、"/cache"、"/dev"等
- //Android的根目录并不像Symbian系统那样分为C盘、D盘、E盘等
- //Android是基于Linux的,只有目录,无所谓盘符
(三)获取系统存储根目录
- File file=Environment.getRootDirectory();//File file=new File("/system");
- File[] fileList=file.listFiles();
- //这里说的系统仅仅指"/system"
- //不包括外部存储的手机存储的范围远远大于所谓的系统存储
(四)获取SD卡存储根目录
- File file=Environment.getExternalStorageDirectory();//File file=new File("/sdcard");
- File[] fileList=file.listFiles();
- //要获取SD卡首先要确认SD卡是否装载
- boolean is=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
- //如果true,则已装载
- //如果false,则未装载
(五)获取data根目录
- File file=Environment.getDataDirectory();//File file=new File("/data");
- File[] fileList=file.listFiles();
- //由于data文件夹是android里一个非常重要的文件夹,所以一般权限是无法获取到文件的,即fileList.length返回为0
(六)获取私有文件路径
- Context context=this;//首先,在Activity里获取context
- File file=context.getFilesDir();
- String path=file.getAbsolutePath();
- //此处返回的路劲为/data/data/包/files,其中的包就是我们建立的主Activity所在的包
- //我们可以看到这个路径也是在data文件夹下
- //程序本身是可以对自己的私有文件进行操作
- //程序中很多私有的数据会写入到私有文件路径下,这也是android为什么对data数据做保护的原因之一
(七)获取文件(夹)绝对路径、相对路劲、文件(夹)名、父目录
- File file=……
- String relativePath=file.getPath();//相对路径
- String absolutePath=file.getAbsolutePath();//绝对路径
- String fileName=file.getName();//文件(夹)名
- String parentPath=file.getParent();//父目录
(八)列出文件夹下的所有文件和文件夹
- File file=……
- File[] fileList=file.listFiles();
(九)判断是文件还是文件夹
- File file=……
- boolean is=file.isDirectory();//true-是,false-否
(十)判断文件(夹)是否存在
- File file=……
- boolean is=file.exists();//true-是,false-否
(十一)新建文件(夹)
- File file=……
- oolean is=file.isDirectory();//判断是否为文件夹
- /*方法1*/
- if(is){
- String path=file.getAbsolutePath();
- String name="ABC";//你要新建的文件夹名或者文件名
- String pathx=path+name;
- File filex=new File(pathx);
- boolean is=filex.exists();//判断文件(夹)是否存在
- if(!is){
- filex.mkdir();//创建文件夹
- //filex.createNewFile();//创建文件
- }
- /*方法2*/
- if(is){
- String path=file.getAbsolutePath();
- String name="test.txt";//你要新建的文件夹名或者文件名
- File filex=new File(path,name);//方法1和方法2的区别在于此
- boolean is=filex.exists();//判断文件(夹)是否存在
- if(!is){
- filex.mkdir();//创建文件夹
- //filex.createNewFile();//创建文件
- }
(十二)重命名文件(夹)
- File file=……
- String parentPath=file.getParent();
- String newName="name";//重命名后的文件或者文件夹名
- File filex=new File(parentPath,newName);//File filex=new File(parentPaht+newName)
- file.renameTo(filex);
(十三)删除文件(夹)
- File file=……
- file.delete();//立即删除
- //file.deleteOnExit();//程序退出后删除,只有正常退出才会删除
简单的文件浏览器
- import java.io.File;
- import java.util.*;
- import android.app.Activity;
- import android.content.Context;
- import android.os.*;
- import android.view.*;
- import android.widget.*;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ImageView.ScaleType;
- public class FileBrowser extends Activity {
- private ListView mainListView=null;
- private List<Map<String,Object>> list=null;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setTitle("文件浏览器");
- mainListView=new ListView(this);
- setContentView(mainListView);
- File file=Environment.getRootDirectory();
- String pathx=file.getAbsolutePath();
- this.setTitle(pathx);
- //android的总目录就是"/"
- list_init("/");
- }
- void list_init(String path){
- File file=new File(path);
- File[] fileList=file.listFiles();
- list=new ArrayList<Map<String,Object>>();
- Map<String,Object> item;
- item=new HashMap<String,Object>();
- if(path.equals("/")){
- item.put("ico",R.drawable.home);
- item.put("name","总目录列表");
- item.put("path","/");
- list.add(item);
- }else{
- item.put("ico",R.drawable.back);
- item.put("name","返回上一级");
- item.put("path",file.getParent());
- list.add(item);
- }
- for(int i=0;i<fileList.length;i++){
- item=new HashMap<String,Object>();
- if(fileList[i].isDirectory()){
- if(fileList[i].list().length<1){
- item.put("ico",R.drawable.file1);
- }else{
- item.put("ico",R.drawable.file2);
- }
- }else{
- item.put("ico",R.drawable.content);
- }
- item.put("name",fileList[i].getName());
- item.put("path",fileList[i].getAbsolutePath());
- list.add(item);
- }
- MyAdapter ma=new MyAdapter(this,list);
- //mainListView=new ListView(this);
- mainListView.setAdapter(ma);
- mainListView.setOnItemClickListener(new OnItemClickListener(){
- public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
- if(arg2>0 && (Integer)(list.get(arg2).get("ico"))==R.drawable.content){
- //非文件夹图标,点击无效
- }else{
- //打开下一级文件目录列表
- list_init((String)(list.get(arg2).get("path")));
- }
- }
- });
- this.setTitle(path);
- }
- public class MyAdapter extends BaseAdapter{
- Context context=null;
- List<Map<String,Object>> list=null;
- MyAdapter(Context context,List<Map<String,Object>> list){
- this.context=context;
- this.list=list;
- }
- public int getCount() {return list.size();}
- public Object getItem(int position) {return position;}
- public long getItemId(int position) {return position;}
- public View getView(int position, View convertView, ViewGroup parent) {
- LinearLayout returnView=new LinearLayout(context);
- returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//注意:ListView.LayoutParams
- //图标
- ImageView iv=new ImageView(context);
- LinearLayout.LayoutParams lp_iv=new LinearLayout.LayoutParams(-2,-2);
- lp_iv.rightMargin=10;
- iv.setLayoutParams(lp_iv);
- iv.setScaleType(ScaleType.CENTER_INSIDE);
- iv.setImageResource((Integer)((list.get(position)).get("ico")));
- returnView.addView(iv);
- //文件名
- TextView name=new TextView(context);
- LinearLayout.LayoutParams lp_tv=new LinearLayout.LayoutParams(-2,-2);
- name.setLayoutParams(lp_tv);
- name.setTextSize(name.getTextSize()+10);
- name.setText((String)(list.get(position).get("name")));
- returnView.addView(name);
- //
- return returnView;
- }
- }
- }
/** * * @return 全部的内部空间大小 */public String getInterTotalSpace_G(){ File path = Environment.getDataDirectory(); StatFs statFs = new StatFs(path.getPath()); double t1 = statFs.getBlockSize(); double t2 = statFs.getBlockCount(); double t = t1 * t2 / 1000/ 1000/1024; return String.format("%.2fG", t);}
/** * * @return 可用的内部空间大小 */public String getInterFreeSpace_G(){ File path = Environment.getDataDirectory(); StatFs statFs = new StatFs(path.getPath()); double t1 = statFs.getAvailableBlocks(); double t2 = statFs.getBlockSize(); double t = t1 * t2 / 1000 / 1000/1024; return String.format("%.2fG", t);}
原文地址:http://blog.csdn.net/mrlixirong/article/details/6800585
http://blog.csdn.net/mrlixirong/article/details/6800073
0 0
- android文件API使用
- Android文件存储API
- android 文件读写api
- Android使用facebook api
- android API使用追踪
- Android如何使用API
- Android使用facebook api
- Android---使用备份API
- Android隐藏API使用
- Android如何使用API
- 使用android隐藏API
- 使用android hide API
- Android--如何使用API
- Android如何使用API
- Android如何使用API
- Android如何使用API
- Android如何使用API
- Android Canvas API使用
- OSX安装express-generator,并创建Nodejs app
- Linux的内存回收和交换
- [李景山php]thinkphp核心源码注释|Firebird.class.php
- 杭电oj-1321
- C#编程.异常处理(Exception Handling Statements)
- android文件API使用
- [Android] 内存泄漏调试经验分享 (一)
- Android Stuido 插件
- [李景山php]thinkphp核心源码注释|Mongo.class.php
- 【HD 5053】the Sum of Cube
- js基础
- maven基础
- 【杭电】1106-排序
- [李景山php]thinkphp核心源码注释|Mysql.class.php