您的位置:首页 > 其它

Andorid存储目录知识总结及文件操作方法封装

2015-04-27 17:02 134 查看
Android的存储系统有三种:

RAM(运行内存,手机断电后数据会丢失),

ROM 就相当于我们电脑的C盘,是手机自带的存储空间,ROM中安装了操作系统的所有东西,我们所说的刷机一般就是刷ROM,而且一般默认的软件安装都是放在这里的,如果不是因为ROM空间不足,最好不要把程序装在SD卡上。因为ROM的读写速度比SD卡快。

SD卡 也就是我们的外部存储。

使用上我们通过android.os.Environment来获得其路径。

File file=Environment.getRootDirector(); //得到系统根目录,

file.getAbsolutePath(); //得到系统根目录的路径。

得到的是 /system

File file1=Environment.getDataDirectroy(); //得到ROM目录

file2.getAbsolutePath(); //得到ROM目录的路径。

得到的是 /data

data文件夹是android中一个非常重要的文件夹,所以一般权限是无法获取到文件的。

FIle file3=Environment.getExteralStorageDirectroy(); //得到外部存储的目录

file3.getAbsolutePath(); //得到外部存储的目录的路径。

要获取SD卡首先要先确定SD卡是否装载

boolean is=Environment.getExteralStorageState().equals(Environment.MEDIA_MOUNTED);

如果为true,则表示已装载。其次我们在SD卡中创建文件和读写文件,需要相应的权限。
<!-- 在SD卡中创建和删除文件的权限-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 在SD卡中写入数据的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


android默认是无法直接操作Data根目录的,只能直接读写程序直接的私有目录,也就是/data/data/Package name/files那什么时候可以直接读写/data呢?当你的APP取得了超级权限,也就系统ROOT之后,那样你的app就是应用级别的了。还需要修改mk文件中的属性为LOCAL_CERTIFICATE :=platform

File file=Context.getFilesDir(); //获得私有文件的目录

file.getAbsolutePath(); //获得私有文件的目录路径

得到的是 /data/data/com.example/testenvironment/files 通用的形式是 /data/data/包/files

我们如果在ROM中存储文件,可以在files目录下创建文件夹和文件来存储数据。

在根据路径(以参数形式传入)创建目录或文件时,需要使用到String.split函数。

函数原型:

public String[] split(String regex,int limit) 根据给定的正则表达式来拆分此字符串。

limit 拆分的次数,最终影响的是数组的长度。

如果limit大于0,那么拆分次数最多为limit-1,且数组长度不会大于limit。

如果limit为非正,那么拆分至不能再拆分为止,数组长度不受限.

如果limit=0,那么拆分至不能再拆分为止,但是要将数组尾部的空字符去掉。

例如:“boo:and:foo”

参数: “o” 1 最多进行0次拆分,那么就是原始字符串了,输出“boo:and:foo”

参数: “o” 2 最多进行1次拆分,输出”b“,“o:and:foo”

参数: “o” 5 最多进行4次拆分,输出“b”,“”,“:and:f”,"",""

参数: “o” -2 最大拆分次数,输出“b”,“”,“:and:f”,"",""

参数: “o” 0 最大拆分次数,去掉尾部空字符 “b”,“”,“:and:f”

函数原型:

public String[] split(String regex) 上面limit=0情况下的调用。

在整个Android应用开发中,我们的一些数据,如图片,服务器端发来的数据需要缓存或保存到本地中,可以保存到ROM也可保存到SD卡中。我们需要经常访问ROM或SD卡,在其中创建目录或文件来存放数据。下面我封装了在ROM/SD卡中创建目录/文件,并在其中保存数据的方法库。
public class FileUtil {

/**
* 在ROM目录下创建文件
* 绝对路径(header)不存在会抛出异常
* @Param header
*            绝对路径,包括私有文件根路径,如/data/data/com.testenvironment/files/meme,/meme是自己创建的存放该app数据的目录
*            整个app使用的绝对路径是一样的,即在/data/data/PackageName/files下创建一个文件夹,该目录需要是存在的,不存在的话,先创建文件夹
* @Param tail
*            相对路径,存放文件的目录,首尾不含/,如 pictures/icon/fileName
* @throws IOException
*/
public static void createFileOnRom(String header, String tail) throws IOException {
//获得绝对路径目录,该目录需要是存在,不存在会抛出异常,需要提前创建
File file=new File(header);
if(!file.exists()){
throw new IOException("The Header Path not exist!");
}
//遍历创建不存在的文件夹
String[] sub=tail.split("/");
String str=header;
for(int i=0;i<sub.length-1;i++){
str=str+"/"+sub[i];
File f=new File(str);
if(!f.exists()){
f.mkdir();
}
}
//创建最终的文件
str=str+"/"+sub[sub.length-1];
File f=new File(str);
if(!f.exists()){
f.createNewFile();
}
}

/**
* 在SD卡中创建文件
* 绝对路径(header)不存在会抛出异常
* @param header
*            绝对路径,包括SD卡根路径,如/storage/emulated/0/meme,/meme自己创建的存放该app数据的目录
* @param tail
*            相对路径,存放文件的目录,首尾 /,如pictures/icon/fileName
* @throws IOException
*/
public static void createFileOnSD(String header, String tail) throws IOException {
//判断是否装载SD卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//绝对路径必须存在
File file=new File(header);
if(!file.exists()){
throw new IOException("The Header Path not exist!");
}
//遍历创建不存在文件夹
String[] sub=tail.split("/");
String str=header;
for(int i=0;i<sub.length-1;i++){
str=str+"/"+sub[i];
File f=new File(str);
if(!f.exists()){
f.mkdir();
}
}
//创建最终的文件
str=str+"/"+sub[sub.length-1];
File f=new File(str);
if(!f.exists()){
f.createNewFile();
}
}
}

/**
* 在ROM目录下创建文件夹
* @throws IOException
*
* @Param header
*            绝对路径,包括私有文件根路径,如/data/data/com.testenvironment/files/meme,/meme是自己创建的存放该app数据的目录
*            整个app使用的绝对路径是一样的,即在/data/data/PackageName/files下创建一个文件夹,该目录需要时存在的,不存在的话,先创建
* @Param tail
*            相对路径,存放文件的目录,首尾不含/,如 pictures/icon/dirName
*/
public static void mkDirOnRom(String header, String tail) throws IOException{
//获得绝对路径目录,必须是存在的
File file=new File(header);
if(!file.exists()){
throw new IOException("The Header Path not exist!");
}
//遍历创建不存在的文件夹
String[] sub=tail.split("/");
String str=header;
for(int i=0;i<sub.length;i++){
str=str+"/"+sub[i];
File f=new File(str);
if(!f.exists()){
f.mkdir();
}
}
}

/**
* 在SD卡上创建文件夹
*
* @param header
*            绝对路径,包括sd卡的根路径,如/storage/emulated/0/meme,
*            /meme是自己创建的存放该app数据的目录
* @param tail
*            相对路径,存放文件的目录,首尾不含/,如pictures/icon/dirName
* @throws IOException
*/
public static void mkDirOnSD(String header, String tail) throws IOException{
//判断是否装载SD卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//绝对路径header必须存在
File file=new File(header);
if(!file.exists()){
throw new IOException("The Header Path not exist!");
}
//遍历创建不存在的文件夹
String[] sub=tail.split("/");
String str=header;
for(int i=0;i<sub.length;i++){
str=str+"/"+sub[i];
File f=new File(str);
if(!f.exists()){
f.mkdir();
}
}
}
}

/**
* 将数据存储到ROM的文件中
*
* @param romPath ROM存储文件路径
* @param b 字符串字节数组
* @throws IOException
*
*/
public static void saveToRomFile(String romPath,byte[] b) throws IOException{
if(romPath.trim().equals("")){
throw new IOException("Path not exist");
}
File file=new File(romPath);
if(!file.exists()){
throw new IOException("file not exist");
}
OutputStream out=new FileOutputStream(file);
out.write(b);
out.close();
}

/**
* 将数据存储到SD卡的文件中
*
* @param sdPath SD存储文件路径
* @param b 字符串字节数组
* @throws IOException
*
*/
public static void saveToSDFile(String sdPath,byte[] b) throws IOException{
if(sdPath.trim().equals("")){
throw new IOException("Path not exist");
}
//判断是否装载SD卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//检测文件是否存在,不存在抛出异常
File file=new File(sdPath);
if(!file.exists()){
throw new IOException("file not exist");
}
OutputStream out=new FileOutputStream(file);
out.write(b);
out.close();
}
}

public static void readFormFile(String sdPath, String romPath) throws IOException {
// 路径判空
if (sdPath.trim().equals("") || romPath.trim().equals("")) {
return;
}
File file = null;
// SD是否装载
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 创建存储文件
file = new File(sdPath);
if (file.exists()) {
InputStream in = new FileInputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
StringBuffer str = new StringBuffer();
while ((len = in.read(buffer)) != -1) {
str.append(new String(buffer));
}
Log.i("TAG", str.toString());
in.close();
}
}
//读取ROM
file = new File(romPath);
if (file.exists()) {
InputStream in = new FileInputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
StringBuffer str = new StringBuffer();
while ((len = in.read(buffer)) != -1) {
str.append(new String(buffer));
}
Log.i("TAG", str.toString());
in.close();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐