android 文件操作方法集合类分享
2013-05-28 13:32
211 查看
http://blog.csdn.net/fenghome/article/details/5668598
android的文件操作要有权限:
SD卡下的文件操作:
1、判断SD卡是否插入
这个类整合了写文件,删除文件,复制文件,搜索文件,判断文件是否存在等,是对一些常用的功能进行封装了。
用于记录和方便以后使用。
android的文件操作要有权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SD卡下的文件操作:
1、判断SD卡是否插入
这个类整合了写文件,删除文件,复制文件,搜索文件,判断文件是否存在等,是对一些常用的功能进行封装了。
public class FileAdapter { private static final String TAG = "FileAdaptor"; /** * 获取指定位置的指定类型的文件 * * @param path * 文件夹路径 * @param type * 文件类型(如“*.jpg;*.png;*.gif”) * @return */ public static void getFileList(String path, String type, final OnFileListCallback onFileListCallback) { new AsyncTask<String, String, String>() { ArrayList<FileInfo> list = new ArrayList<FileInfo>(); @Override protected void onPostExecute(String result) { onFileListCallback.SearchFileListInfo(list); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String path = params[1].substring(params[1] .lastIndexOf(".") + 1); File file = new File(params[0]); scanSDCard(file,path,list); return null; } }.execute(path, type, ""); } /** * 扫描完成后的回调,获取文件列表必须实现 * * @author cola * */ public interface OnFileListCallback { /** * 返回查询的文件列表 * @param list 文件列表 */ public void SearchFileListInfo(List<FileInfo> list); } private static void scanSDCard(File file, String ext, ArrayList<FileInfo> list) { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File tmp = files<i>; if (tmp.isFile()) { String fileName = tmp.getName(); String filePath = tmp.getName(); if (fileName.indexOf(".") >= 0) { fileName = fileName.substring(fileName .lastIndexOf(".") + 1); if (ext != null && ext.equalsIgnoreCase(fileName)) { AspLog.i(TAG, filePath); FileInfo info = new FileInfo(); info.fileName = filePath; info.filePath = tmp.getAbsolutePath(); list.add(info); } } } else scanSDCard(tmp, ext, list); } } } else { if (file.isFile()) { String fileName = file.getName(); String filePath = file.getName(); if (fileName.indexOf(".") >= 0) { fileName = fileName .substring(fileName.lastIndexOf(".") + 1); if (ext != null && ext.equalsIgnoreCase(fileName)) { AspLog.i(TAG, filePath); FileInfo info = new FileInfo(); info.fileName = filePath; info.filePath = file.getAbsolutePath(); list.add(info); } } } } } /** * 判断 */ public static boolean isSdCardExist() { return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); } /** * 目录相关 */ public static String getSdCardRootDir() { return Environment.getExternalStorageDirectory().getPath() + "/"; } public static String getCacheDir(Context ctx) { // /data/data/<package name>/cache return ctx.getCacheDir().getPath() + "/"; } public static String getFilesDir(Context ctx) { // /data/data/<package name>/files return ctx.getFilesDir().getPath() + "/"; } public static String getSharedPrefDir(Context ctx) { String path = "/data/data/com.timedee.calendar/shared_prefs/"; mkDir(path); return path; } public static String getSdDataDir() { return getSdCardRootDir() + "timedee/.data/"; } public static String getBackupDir() { return getSdCardRootDir() + "timedee/.backup/"; } public static boolean mkDir(String path) { File dir = new File(path); boolean res = dir.mkdirs(); return res; } /** * 文件操作相关 */ public static boolean writeFile(String path, InputStream is) { boolean result = false; FileOutputStream os = null; BufferedOutputStream bos = null; try { File file = new File(path); os = new FileOutputStream(file, false); bos = new BufferedOutputStream(os); int readLen = 0; byte[] buf = new byte[1024]; while ((readLen = is.read(buf)) != -1) { bos.write(buf, 0, readLen); } bos.flush(); bos.close(); os.close(); result = true; } catch (Exception e) { e.printStackTrace(); } finally { try { bos.close(); os.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } public static boolean writeTextFile(String path, String data) { boolean result = false; FileWriter fw = null; try { File file = new File(path); fw = new FileWriter(file); fw.write(data); fw.close(); result = true; } catch (Exception e) { e.printStackTrace(); } finally { try { fw.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } public static InputStream readFile(String path) { File file = new File(path); FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (Exception e) { } return fis; } public static boolean CopyFile(String fromFile, String toFile) { try { InputStream fosfrom = new FileInputStream(fromFile); OutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[4096]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosfrom.close(); fosto.close(); bt = null; return true; } catch (Exception ex) { return false; } } public static boolean CopyAssetFile(Context ctx, String fromFile, String toFile) { try { InputStream fosfrom = ctx.getAssets().open(fromFile); OutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[4096]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosfrom.close(); fosto.close(); bt = null; return true; } catch (Exception ex) { return false; } } public static boolean deleteFile(String path) { try { File file = new File(path); return file.delete(); } catch (Exception ex) { return false; } } public static boolean isFileExist(String path) { try { File file = new File(path); return file.exists(); } catch (Exception ex) { } return false; } }
用于记录和方便以后使用。
相关文章推荐
- android 文件操作方法集合类分享
- android 文件操作方法集合类分享
- (转)[Android分享] Android中用Ant把ndk的so文件打包进apk
- 跟大家分享下Android布局文件layout.xml的一些属性值。
- 分享一个牛哄哄的Android工具集合类
- Google Android开发者文档系列-创建有内容分享特性的应用之共享文件
- Android中的多种文件读写操作方法
- Android应用中实现选择本地文件与目录的实例分享
- Android 实现文件分享功能(共享多个文件)
- Android应用中使用及实现系统“分享”功能(多个文件,多种媒体格式触发)
- Android官方开发文档Training系列课程中文版:分享文件之分享一个文件
- Google Android开发者文档系列-创建有内容分享特性的应用之获取文件信息
- Android Intent分享文件|分享图片|分享文字|功能的实现
- android studio svn分享时的忽略文件
- Android中的多种文件读写操作方法
- 【分享】兼容ie6-9和现代浏览器以及ios,android,pad等触屏设备的异步文件上传插件
- Android文件/文件夹批量蓝牙分享
- Android中的多种文件读写操作方法
- android中一些文件的操作方法
- Android中init.rc文件的解析 分享