Android Data Storage(数据存储)之Internal Storage

可以直接保存文件在你的设备内部存储。通常情况下,文件保存在内存属于你应用程序私有的,其他应用程序不能访问(用户也不能)。当你的应用程序被卸载时,这些文件也被移除。


文件一般存储在/data/data/<package name>/files目录下

向内存创建和写入私有文件步骤:
1、  调用 openFileOutput(name,mode)方法文件名称和操作模式   方法返回一个 FileOutputStream对象
2、  用 write()方法写入文件
3、  用 close()方法关闭流

For example:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

其他可用模式:

MODE_APPEND:     如果文件存在,在文件末尾写入,  而不是消除它    不存在就创建
MODE_WORLD_READABLE:   外部应用程序可以去读
MODE_WORLD_WRITEABLE:    外部应用程序可以写入

从内存设备中读取文件:

1、  调用openFileInput(name) 方法通过文件名字读取文件    方法返回FileInputStream对象
2、  用read()方法从文件中读取字节
3、  用close()方法关闭流

如果你想在编译时保存一个静态文件在你的应用程序,保存这个文件在你的项目 res/raw/ 目录中。  你可以调用openRawResource()方法打开它,通过 R.raw.<filename> 资源ID。  这个方法返回一个 InputStream 对象 你可以使用它用来读取这个文件(但不能写原始文件)


缓存数据:

如果你想缓存一些数据,而不是持续储存,当你的应用程序将要保存临时文件存储时,你可能使用getCacheDir() 方法打开文件这个文件代表你的内部文件夹。

当设备内存空间不足时,Android也许会删除这个存储文件释放空间。  然而,你不能依赖于系统去清理这些文件,  你应始终保持缓存文件和保持合理的缓存空间,例如1MB。 当用户卸载应用程序时  这些文件也应该移除。


使用的一些方法:

getFilesDir(): 获取文件的绝对路径(data/data/<package name>/files/)

getDir(String name, int mode):获取或创建name对应的文件

deleteFile(String name): 通过文件名删除文件

fileList():获取数据文件夹的全部文件

/**
 * 内部存储(文件存储管理)
 */
public class InternalStorage {
    /*  内存设备中文件存储路径一般在项目下的files文件中 data/data/<package name>/files/  */
    Context context_;
    private static final int MODE_DEVICES_FILES = 0X0001;
    private static final int MODE_DEVICES_CACHE = 0X0002;
    
    public InternalStorage(Context context) {
        this.context_ = context;
    }
    
    /**
     * 获取需要访问或创建的文件(判断文件存储空间)
     * @param name
     * @param mode
     * @return
     */
    private File getFile(String name, int mode){
        File file = null;
        if (mode == MODE_DEVICES_FILES) {
            file = new File(context_.getFilesDir(), name);
        } else if(mode == MODE_DEVICES_CACHE) {
            file = new File(context_.getCacheDir(), name);
        }
        return file;
    }
    
    public void save(String name,String content){
        save(name, content, MODE_DEVICES_FILES);
    }
    
    /**
     * 保存文件到内部存储设备
     * @param name
     */
    private void save(String name, String content, int mode){
        File file = getFile(name, mode);
        
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(file);
            fos.write(content.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public String get(String name){
        return get(name, MODE_DEVICES_FILES);
    }
    
    /**
     * 获取文件内容(先判断文件是否存在)
     * @param name
     * @return
     */
    private String get(String name, int mode){
        if (isExist(name, mode)) {
            File file = getFile(name, mode);
            
            StringBuffer content = new StringBuffer();
            FileInputStream fis = null;
            ByteArrayOutputStream baos = null;
            try {
                fis = new FileInputStream(file);
                baos = new ByteArrayOutputStream();
                
                byte[] bytes = new byte[1024];
                
                while (fis.read(bytes) != -1) {
                    baos.write(bytes);
                }
                
                content.append(baos.toString());
                
                return content.toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != fis) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            System.out.println("------- >>> " + name + " : 文件不存在!");
        }
        return "";
    }
    
    /**
     * 在内部存储设备文件中追加内容(如果文件存在,否则创建文件)
     * @param name
     * @param content
     */
    public void append(String name, String content){
        FileOutputStream fos = null;
        try {
            fos = context_.openFileOutput(name, Context.MODE_APPEND);
            fos.write(content.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 删除内部存储设备指定的文件
     * @param name
     */
    public boolean delete(String name){
        boolean isDelete; 
        
        try {
            isDelete = context_.deleteFile(name);
            return isDelete;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    /**
     * 获取数据文件夹下全部文件名称
     * @return
     */
    public String[] queryAllFile(){
        return context_.fileList();
    }
    
    /**
     * 判断文件是否存在
     * @param name
     * @return
     */
    public boolean isExist(String name, int mode){
        String[] names = null;
        if (mode == MODE_DEVICES_FILES) {
            names = queryAllFile();
        } else if(mode == MODE_DEVICES_CACHE){
            names = context_.getCacheDir().list();
        }
        
        if (null != names) {
            for (String string : names) {
                if (string.equals(name)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    
    /*
     *   临时存储 (其他方法相同)
     */
    
    public void saveCacheFile(String name, String content){
        save(name, content, MODE_DEVICES_CACHE);
    }
    
    public String getCacheFile(String name){
        return get(name, MODE_DEVICES_CACHE);
    }
}  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值