Android FileUtils 文件操作类
系统路径
Context.getPackageName(); // 用于获取APP的所在包目录
Context.getPackageCodePath(); //来获得当前应用程序对应的apk文件的路径
Context.getPackageResourcePath(); // 获取该程序的安装包路径
Context.getDatabasePath(); //返回通过Context.openOrCreateDatabase创建的数据库文件 Environment.getDataDirectory().getPath(); // 获得根目录/data
Environment.getDownloadCacheDirectory().getPath(); //获得缓存目录/cache
Environment.getExternalStorageDirectory().getPath(); //获得SD卡目录/mnt/sdcard
Environment.getRootDirectory().getPath(); // 获得系统目录/system
//File.separator 代表 "/"
文件操作
String path = File.getPath();//获得文件或文件夹的绝对路径
String path = File.getAbsoultePath();//获得文件或文件夹的相对路径 String parentPath = File.getParent();//获得文件或文件夹的父目录 String Name = File.getName();//获得文件或文件夹的名称 File.mkDir(); //建立文件夹
File.createNewFile();//建立文件 File[] files = File.listFiles();//列出文件夹下的所有文件和文件夹名 File.isDirectory();//true是文件夹,false是文件 File.renameTo(dest);//修改文件夹和文件名 File.delete();//删除文件夹或文件
资源文件assets和RW
res/raw:文件会被映射到R.java文件中,访问的时候直接通过资源ID访问,没有有目录结构
assets:不会映射到R.java文件中,通过AssetManager来访问,能有目录结构
//raw:
InputStream is =getResources().openRawResource(R.raw.filename); //assets:
AssetManager am = getAssets();
InputStream is = am.open("filename");
从资源文件中获取Bitmap
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.ico);
FileUtils文件操作类
public class FileUtils {
//检查SDCard存在并且可以读写
public static boolean isSDCardState(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 判断文件是否已经存在
*@param fileName 要检查的文件名
* @return boolean, true表示存在,false表示不存在
*/
public static boolean isFileExist(String fileName) {
File file = new File("绝对路径" + fileName);
return file.exists();
}
/**
* 新建目录
* @param path 目录的绝对路径
* @return 创建成功则返回true
*/
public static boolean createFolder(String path){
File file = new File(path);
return file.mkdir();
}
/**
* 创建文件
*@param path 文件所在目录的目录名
* @param fileName 文件名
* @return 文件新建成功则返回true
*/
public static boolean createFile(String path, String fileName) {
File file = new File(path + File.separator + fileName);
if (file.exists()) {
return false;
} else {
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
/**
* 删除单个文件
* @param path 文件所在的绝对路径
* @param fileName 文件名
* @return 删除成功则返回true
*/
public static boolean deleteFile(String path, String fileName) {
File file = new File(path + File.separator + fileName);
return file.exists() && file.delete();
}
/**
* 删除一个目录(可以是非空目录)
* @param dir 目录绝对路径
*/
public static boolean deleteDirection(File dir) {
if (dir == null || !dir.exists() || dir.isFile()) {
return false;
}
for (File file : dir.listFiles()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
deleteDirection(file);//递归
}
}
dir.delete();
return true;
}
/**
* 将字符串写入文件
*@param text 写入的字符串
* @param fileStr 文件的绝对路径
* @param isAppend true从尾部写入,false从头覆盖写入
*/
public static void writeFile(String text, String fileStr, boolean isAppend) {
try {
File file = new File(fileStr);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream f = new FileOutputStream(fileStr, isAppend);
f.write(text.getBytes());
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 拷贝文件
*@param srcPath 绝对路径
* @param destDir 目标文件所在目录
* @return boolean true拷贝成功
*/
public static boolean copyFile(String srcPath, String destDir){
boolean flag = false;
File srcFile = new File(srcPath); // 源文件
if (!srcFile.exists()){
Log.i("FileUtils is copyFile:","源文件不存在");
return false;
}
// 获取待复制文件的文件名
String fileName = srcPath.substring(srcPath.lastIndexOf(File.separator));
String destPath = destDir + fileName;
if (destPath.equals(srcPath)){
Log.i("FileUtils is copyFile:","源文件路径和目标文件路径重复");
return false;
}
File destFile = new File(destPath); // 目标文件
if (destFile.exists() && destFile.isFile()){
Log.i("FileUtils is copyFile:","该路径下已经有一个同名文件");
return false;
}
File destFileDir = new File(destDir);
destFileDir.mkdirs();
try{
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] buf = new byte[1024];
int c;
while ((c = fis.read(buf)) != -1) {
fos.write(buf, 0, c);
}
fis.close();
fos.close();
flag = true;
}catch (IOException e){
e.printStackTrace();
}
return flag;
}
/**
* 重命名文件
*@param oldPath 旧文件的绝对路径
* @param newPath 新文件的绝对路径
* @return 文件重命名成功则返回true
*/
public static boolean renameTo(String oldPath, String newPath){
if (oldPath.equals(newPath)){
Log.i("FileUtils is renameTo:","文件重命名失败:新旧文件名绝对路径相同");
return false;
}
File oldFile = new File(oldPath);
File newFile = new File(newPath);
return oldFile.renameTo(newFile);
}
/**
* 计算某个文件的大小
*@param path 文件的绝对路径
*@return 文件大小
*/
public static long getFileSize(String path){
File file = new File(path);
return file.length();
}
/**
*计算某个文件夹的大小
*@param file 目录所在绝对路径
* @return 文件夹的大小
*/
public static double getDirSize(File file) {
if (file.exists()) {
//如果是目录则递归计算其内容的总大小
if (file.isDirectory()) {
File[] children = file.listFiles();
double size = 0;
for (File f : children)
size += getDirSize(f);
return size;
} else {//如果是文件则直接返回其大小,以“兆”为单位
return (double) file.length() / 1024 / 1024;
}
} else {
return 0.0;
}
}
/**
* 获取某个路径下的文件列表
* @param path 文件路径
* @return 文件列表File[] files
*/
public static File[] getFileList(String path) {
File file = new File(path);
if (file.isDirectory()){
File[] files = file.listFiles();
if (files != null){
return files;
}else{
return null;
}
}else{
return null;
}
}
/**
* 计算某个目录包含的文件数量
*@param path 目录的绝对路径
* @return 文件数量
*/
public static int getFileCount(String path){
File directory = new File(path);
File[] files = directory.listFiles();
return files.length;
}
/**
* 获取SDCard 总容量大小(MB)
*@param path 目录的绝对路径
* @return 总容量大小
* */
public long getSDCardTotal(String path){
if(null != path&&path.equals("")){
StatFs statfs = new StatFs(path);
//获取SDCard的Block总数
long totalBlocks = statfs.getBlockCount();
//获取每个block的大小
long blockSize = statfs.getBlockSize();
//计算SDCard 总容量大小MB
return totalBlocks*blockSize/1024/1024;
}else{
return 0;
}
}
/**
* 获取SDCard 可用容量大小(MB)
*@param path 目录的绝对路径
* @return 可用容量大小
* */
public long getSDCardFree(String path){
if(null != path&&path.equals("")){
StatFs statfs = new StatFs(path);
//获取SDCard的Block可用数
long availaBlocks = statfs.getAvailableBlocks();
//获取每个block的大小
long blockSize = statfs.getBlockSize();
//计算SDCard 可用容量大小MB
return availaBlocks*blockSize/1024/1024;
}else{
return 0;
}
}
}
Android FileUtils 文件操作类的更多相关文章
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
- File 文件操作类 大全
File 文件操作类 大全 许多人都会对文件操作感到很难 我也是 但是一个好的项目中必定会涉及到文件操作的 文件的复制 粘贴 等等等 公司大佬写了 一个文件操作的工具类 感觉还是棒棒的啦 ...
- [C#] 常用工具类——文件操作类
/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...
- 文件操作类CFile
CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...
- asp.net文件操作类
/** 文件操作类 **/ #region 引用命名空间 using System; using System.Collections.Generic; using System.Text; usin ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- java csv 文件 操作类
一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...
- Qt5:Qt文件操作类 QFile
在QT中,操作文件一般不使用C++提供的文件操作类 , 因为操作文件的时候,要用到C++提供的 string 类,而在QT中使用的是Qt自己实现的一个string类 QString .在Qt中使用C+ ...
- C# 文件操作类大全
C# 文件操作类大全 时间:2015-01-31 16:04:20 阅读:1724 评论:0 收藏:0 [点我收藏+] 标签: 1.创建文件夹 //usin ...
随机推荐
- javascript Location对象属性和方法
Location对象 Location对象包含有关当前URL的信息 https://blog.csdn.net/u010953692/article/details/80901035#3 https( ...
- Tools - OpenSSL
OpenSSL http://www.openssl.org/ OpenSSL is an open source project that provides a robust, commercial ...
- Testing - 软件测试知识梳理 - 探索性测试
定义 探索性测试(Exploratory Testing)是一种自由的软件测试风格,强调测试人员同时展开测试学习,测试设计,测试执行和测试结果评估等活动,以持续优化测试工作. 其特征有:即兴发挥,快速 ...
- SpringBoot跨域小结
前言:公司的SpringBoot项目出于某种原因,经常样处理一些跨域请求. 一.以前通过查阅相关资料自己写的一个处理跨域的类,如下. 1.1首先定义一个filter(拦截所有请求,包括跨域请求) pu ...
- Liferay7 BPM门户开发之12:acitiviti和liferay用户权限体系集成
写到第12章才出现Liferay的内容,希望可以厚积薄发. 我们的目标是不使用不维护Activiti的用户组织架构,只维护Liferay的体系,这样的好处是非常明显的,即不用做组织架构的同步工作. 原 ...
- 国外程序员整理的 C++ 资源大全 (zt)
关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...
- 如何在IDEA里给大数据项目导入该项目的相关源码(博主推荐)(类似eclipse里同一个workspace下单个子项目存在)(图文详解)
不多说,直接上干货! 如果在一个界面里,可以是单个项目 注意:本文是以gradle项目的方式来做的! 如何在IDEA里正确导入从Github上下载的Gradle项目(含相关源码)(博主推荐)(图文详解 ...
- Git学习系列之Windows上安装Git详细步骤(图文详解)
前言 最初,Git是用于Linux下的内核代码管理.因为其非常好用,目前,已经被成功移植到Mac和Windows操作系统下. 鉴于大部分使用者使用的是Windows操作系统,故,这里详细讲解Windo ...
- PostgreSQL 使用小点
1.timestamp 有分时区和无时区(减少使用),8个字节,包含日期和时间,范围是:公元前4713 - 公元294276.底层存储的是时间戳,这对任何地区的都一样,而具体时间显示则可从数据库层面设 ...
- 经典面试题SALES TAXES思路分析和源码分享
题目: SALES TAXES Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and ...