Android——文件操作

记录一下Android常用的文件操作

1.自动级联创建文件
/**
* 自动级联创建一个文件
*
* @param filePath
* @return
* @throws IOException
* @throws
*/
public static File createFile(String filePath) throws IOException {
File file = null;
if (filePath != null) {
file = new File(filePath);
// 如果文件不存在就创建一个
if (!file.exists()) {
// 如果路径不存在,则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
}
return file;
}


2.按照路径删除一个文件
/**
* 按照路径删除一个文件
*
* @param filePath
* @throws
*/
public static void deleteFile(String filePath) {
if (filePath != null) {
File file = new File(filePath);
// file.deleteOnExit();此方法有时无效
if(file.exists()){
file.delete();
}
}
}


3.删除一个文件
/**
* 删除一个文件
*
* @param filePath
* @throws
*/
public static void deleteFile(File file) {
if (file != null) {
// file.deleteOnExit();
if(file.exists()){
file.delete();
}
}
}


4.保存一个字符串到文件里面去
/**
* 保存一个字符串到文件里面去
*
* @param filePath
* @param content
* @throws IOException
* @throws
*/
public static void saveString(String filePath, String content)
throws IOException {
if (filePath != null) {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
// 创建文件失败,则退出操作
if (!file.exists()) {
return;
}
FileWriter fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
bw = null;
fw.close();
fw = null;
}
}


5.向文件追加写入
/**
* 追加文件写入
*
* @param filePath
* @param content
* @throws IOException
* @throws
*/
public static void addString(String filePath, String content)
throws IOException {
if (filePath != null) {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
// 文件追加写入
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
bw = null;
fw.close();
fw = null;
}
}


6.从本地文件中获取字符串
/**
* 从本地文件中获取字符串
*
* @param filePath
* @return
* @throws IOException
* @throws
*/
public static String getString(String filePath) throws IOException {
String result = null;
if (filePath != null) {
StringBuffer sb = null;
File file = new File(filePath);
if (!file.exists()) {
return null;
}
FileReader fw = new FileReader(file);
BufferedReader br = new BufferedReader(fw);
sb = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
br.close();
fw.close();
result = sb.toString();
}
return result;
}


7.创建文件夹
/**
* 创建文件夹
*
* @param dirPath
* @throws
*/
public static void createDir(String dirPath) {
if (dirPath != null) {
File file = new File(dirPath);
// 如果不存在,并且不是文件类型的,就认为是文件夹
if (!file.isFile() && !file.exists()) {
// 级联创建文件夹
file.mkdirs();
}
}
}


8.删除文件夹
/**
* 删除文件夹
*
* @param dirPath
* @throws
*/
public static void deleteDir(String dirPath) {
if (dirPath != null) {
File file = new File(dirPath);
// file.deleteOnExit();//此方法失效
if(file.exists()){
file.delete();
}
}
}


9.复制文件,src表示源文件路径,targ表示目标文件路径
/**
* 复制文件,src表示源文件路径,targ表示目标文件路径
*
* @throws IOException
* @throws FileNotFoundException
* @param src
* @param targ
* @throws
*/
public static void copyFile(String src, String targ) throws IOException {
if (src != null && targ != null) {
File srcFile = new File(src);
File targFile = new File(targ);
if (srcFile.exists()) {
if (!targFile.exists()) {
targFile.createNewFile();
}
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(targFile);
int len = 0;
byte[] buf = new byte[2048];
while ((len = fis.read(buf, 0, 2048)) > 0) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
fos = null;
fis.close();
fis = null;
}
}
}


10.从文件地址截取文件名称
/**
* 从文件地址截取文件名称
*
* @param url
* @return
*/
public static final String getFilenameFromUrl(String url) {
if (url == null) {
return null;
}
int index = url.lastIndexOf(‘/’);
if (index < 0) {
return url;
}
if (index == (url.length() - 1)) {
return “”;
}
int pos = url.lastIndexOf(‘.’);
if (pos < 0) {
return url.substring(index + 1);
}
else {
return url.substring(index + 1, pos);
}
}


11.判断一个本地文件是否存在
/**
* 判断一个本地文件是否存在
*
* @param filePath
* @return
* @throws
*/
public static boolean isExistFile(String filePath) {
boolean result = false;
if (filePath != null) {
File file = new File(filePath);
if (file.exists()) {
result = true;
}
}
return result;
}


12.递归删除所有文件
/**
* 递归删除所有文件
* @param file
*/
public static void deleteAll(File file) {
if (file == null) {
return;
}
File[] fileList = file.listFiles();
if(fileList != null){
for (File tempFile : fileList) { // 递归删除文件
if (tempFile.isFile()) {
tempFile.delete();
} else {
deleteAll(tempFile);
}
}
}
if (file.exists() && file.isDirectory()) { // 删除文件夹
file.delete();
}
}


13.解压文件到指定目录
/**
* 解压文件到指定目录
* @param zipPath
* @param descDir
*/
@SuppressWarnings(“rawtypes”)
public static void unZipFiles(String zipPath,String descDir)throws IOException{
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(new File(zipPath));
for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
//Log.i(“space”,”–>>utf-8转换前”+entry.getName());
String zipEntryName = new String(entry.getName().getBytes(), “utf-8”);
//Log.i(“space”,”–>>utf-8转换后”+zipEntryName);
InputStream in = zip.getInputStream(entry);
String outPath = (descDir+zipEntryName).replaceAll(“\*”, “/”);;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf(‘/’)));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
//输出文件路径信息
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
}


如有错误欢迎指出,谢谢

android文件架构详解 cache : 是缓存临时文件夹,据说是除了T-mobile的OTA更新外,别无用处。 红色标记的两个文件是debug模式下产生的 data : 存放用户安装的软件以及各种数据。 default.prop : 默认配置文件 dev : 设备节点文件的存放地 etc : 指向 /system/etc ,配置文件存放目录 init : 系统启动到文件系统的时候第一个运行的程序。 init.goldfish.rc : 初始化文件 init.rc : 初始化文件 proc : /proc文件系统下的多种文件提供的系统信息不是针对某个特定进程的,而是能够在整个系统范围的上下文中使用。 root : 为空 。 sbin: 只放了一个用於调试的adbd程序 sdcard: 是SD卡中的FAT32文件系统挂载的目录 sqlite_stmt_journals: 一个根目录下的tmpfs文件系统,用於存放临时文件数据。 sys : 用於挂载 sysfs文件系统。 在设备模型中,sysfs文件系统用来表示设备的结构.将设备的层次结构形象的反应到用户空间中.用户空间可以修改sysfs中的文件属性来修改设备的属性值 system :系统中的大部分东西都在这各目录下,很重要的一个目录文件 system目录是在Android文件系统占有及其重要的位置,基本上所有的工具和应用程序都在这个目录下,我看来是一个真正的rootfs。他在Android手机中存放在nandflash的 mtd3中,是一个yaffs2文件系统,在启动时被挂载在root的/system目录下,其中包含有: # ls -a -l /system drwxr-xr-x root 208 1970-01-01 08:00 xbin drwxr-xr-x root root 1970-01-01 08:00 modules drwxr-xr-x root root 2010-06-23 09:39 framework drwxr-xr-x root root 2010-06-23 09:39 fonts drwxr-xr-x root root 2010-06-23 09:39 etc -rw-r--r-- root root 2197 2010-06-23 09:39 build.prop drwxr-xr-x root root 2010-06-23 09:39 media drwxr-xr-x root shell 2010-06-23 09:39 bin drwxr-xr-x root root 2010-06-23 09:39 usr drwxr-xr-x root root 2010-06-23 09:39 app drwxr-xr-x root root 2010-06-23 09:39 lost+found drwxr-xr-x root root 2010-06-23 09:39 lib drwxr-xr-x root root 2010-06-23 09:39 sd -rw-r--r-- root root 1452010-06-23 09:39 init.rc # xbin :下放了很多系统管理工具,这些工具不是到toolbox的链接,每个都是可执行程序。如果你看到这些命令你会发现他们根本不常用,他们都是为系统管理员准备的,是一些系统管理和配置工具。这个文件夹的作用相当於标准Linux文件系统中的 /sbin。 modules:使用来存放内核模块(主要是fs和net)和模块配置文件的地方。 framework: 是JAVA平台的一些核心文件,属於JAVA平台系统框架文件。里面的文件都是.jar和.odex文件。 备注:什么是odex文件? odex是被优化过的JAVA程序文件,体积通常是.jar的4倍左右。执行效率比.jar高。 fonts :字体库文件的存放目录。 etc :这里存放了系统中几乎所有的配置文件,根目录下的/etc就链结於此。 build.prop :是一个属性文件,在Android系统中.prop文件很重要,记录了系统的设置和改变,类似於/etc中的文件。 media :里面主要是存放了系统的铃声的,分为 notifications(通知)、ui(界面)、alarms(警告)和ringtones(铃声),里面都是.ogg音频文件。 bin :是存放用户常用的工具程序的,其中大部分是到toolbox的链接(类似嵌入式Linux中的busybox)。toolbox应该是 google简化版的busybox。 usr :用户的配置文件,如键盘布局、共享、时区文件等等。您可以cat 来看看。 app :存放的是Android系统自带的JAVA应用程序。 lost+found :yaffs文件系统固有的,类似回收站的文件夹,只有是yaffs文件系统都会有。 lib :存放几乎所有的共享库(.so)文件。 sd :SD卡中的EXT2分区的挂载目录 init.rc :一个初始化脚本,用於将/system/modules和/system/xbin挂载为cramfs,避免系统被无意破坏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值