您的位置:首页 > 移动开发 > Android开发

Android 读取手机内存实例

2014-10-11 09:57 246 查看
我们的这个小例子主要讲的就是怎么样读取手机内存里的文件,这个在Android开发当中非常重要的一点,这个要是掌握不好的话,我们就没法把数据库里保存的数据给读出来,也就是说我们我们每一次玩游戏的时候,我们的数据库都是最原始的数据,这样就会给我们带来很多的麻烦。如果出现这样的事情,那你的应用就是非常烂的一个应用,会没有人使用的。不多说了,我们来看看代码:
复制到剪贴板Java代码
public static InputStream readInternalFileInputStream(Context context,String fileName){

/**

* 读取手机内存文件

*

*/

try{

FileInputStream fis = context.openFileInput(fileName);

return fis;

}catch(Exception e){

return null;

}

}

public static String readInternalFile(Context context,String fileName){

/**

* 读取手机内存文件

*

*/

try{

byte[] buffer = new byte[512];

int read =0;

StringBuffer stringbuffer = new StringBuffer();

FileInputStream fis = context.openFileInput(fileName);

do{

read = fis.read(buffer);

if(read>0)

stringbuffer.append(new String(buffer, 0, read, "utf-8"));

}while(read!=-1);

fis.close();

return stringbuffer.toString();

}catch(Exception e){

return null;

}

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