展会信息港展会大全

Android读取手机内存
来源:互联网   发布日期:2016-01-14 12:29:40   浏览:2341次  

导读:下面给出一个小例子来说明如何Android读取手机内存里的文件,这在Android开发中是比较重要的一部分,如何要是掌握不好的话,我们就没法把数据库里保存的数据给读出来,也就是说我们我们每一次玩游戏的时候,我们......

下面给出一个小例子来说明如何Android读取手机内存里的文件,这在Android开发中是比较重要的一部分,如何要是掌握不好的话,我们就没法把数据库里保存的数据给读出来,也就是说我们我们每一次玩游戏的时候,我们的数据库都是最原始的数据,这样就会给我们带来很多的麻烦。如果出现这样的事情,那么你的应用就是非常烂的,代码如下:

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;

}

}

赞助本站

人工智能实验室

相关热词: 读取 手机手机内存

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港