Android编码文件操作相关工具类

来源:互联网 发布:子域名查询工具3.0 编辑:程序博客网 时间:2024/06/06 02:41

第一,app应用数据保存路径

Android应用的数据是保存在data/data/包名目录下的。

/** * 生成包的文件夹用于保存数据库 */public void setPackageDir(Context context){try {BS.packageDBMir=new File("/data/data/"+context.getPackageName()+"/databases");if(!BS.packageDBMir.exists()){BS.packageDBMir.mkdir();}} catch (Exception e) {Log.e("<<<<<", "setPageDir", e);}}其他files,share_pref类似




第二,对数据保存的文件的相关操作


public class FileExists {/** * @category 判断文件夹是否存在 * @param path 文件路径 */public  boolean isExists(String path){boolean flag=false;try {File file=new File(path);if(file.exists()){flag=true;}} catch (Exception e) {flag=false;e.printStackTrace();}return flag;}}/** * 返回一个文件名称为sharename的SharedPreferences对象 * @param    context * @param    sharename * @return */public SharedPreferences getShare(Context context, String sharename) {try {if (null != context && null != sharename) return context.getSharedPreferences(sharename, Context.MODE_PRIVATE);return null;} catch (Exception e) {Log.e(this, "getShare", e);return null;}}/** * 检测是否有shareName名称的SharedPreferences对象文件 * @param context * @param shareName 文件名称  不带有后缀的 * @return */public boolean isShareExist(Context context,String shareName){try {if(null!=shareName&&!"".equals(shareName)&&!"null".equals(shareName)){return new File("/data/data/"+context.getPackageName()+"/shared_prefs/"+shareName+".xml").exists();}return false;} catch (Exception e) {Log.e(this, "isShareExist", e);return false;}}/** * 编辑SharedPreferences文件 * @param share * @param key       String类型 * @param value */public void editorSharePre (SharedPreferences share,String key,String value){try {if (null != share && null != key && null != value) {editor = share.edit();editor.putString(key, value);editor.commit();editor.clear();}} catch (Exception e) {Log.e(this, "editorSharePre", e);return;}}/** * 编辑SharedPreferences文件 * @param share * @param key       Integer类型 * @param value */public void editorSharePre (SharedPreferences share,String key,Integer value){try {if (null != share && null != key && null != value) {editor = share.edit();editor.putInt(key, value);editor.commit();editor.clear();}} catch (Exception e) {Log.e(this, "editorSharePre", e);return;}}/** * 编辑SharedPreferences文件 * @param share * @param key       Boolean类型 * @param value */public void editorSharePre (SharedPreferences share,String key,Boolean value){try {if (null != share && null != key && null != value) {editor = share.edit();editor.putBoolean(key, value);editor.commit();editor.clear();}} catch (Exception e) {Log.e(this, "editorSharePre", e);return;}}
/**<span style="white-space:pre"></span> * 从Raw文件下拷贝至手机指定路径<span style="white-space:pre"></span> * @param context<span style="white-space:pre"></span> * @param filepath<span style="white-space:pre"></span> * @param raw<span style="white-space:pre"></span> */<span style="white-space:pre"></span>public void copyFileToAppFromRaw(Context context,String filepath,int raw){<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>File file=new File("/data/data/"+context.getPackageName()+"/databases");<span style="white-space:pre"></span>if(!file.exists()){<span style="white-space:pre"></span>file.mkdirs();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>file = new File(filepath);<span style="white-space:pre"></span>BufferedInputStream inputStream = new BufferedInputStream(context.getResources().openRawResource(raw));<span style="white-space:pre"></span>BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));<span style="white-space:pre"></span>byte[] buff = new byte[20*1024];<span style="white-space:pre"></span>int len;<span style="white-space:pre"></span>while ((len = inputStream.read(buff)) > 0) {<span style="white-space:pre"></span>outputStream.write(buff, 0, len);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>outputStream.flush();<span style="white-space:pre"></span>inputStream.close();<span style="white-space:pre"></span>outputStream.close();<span style="white-space:pre"></span>} catch (Exception e) {<span style="white-space:pre"></span><span style="white-space:pre"></span>return;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}


0 0
原创粉丝点击