Android 本地存储

来源:互联网 发布:越南人 知乎 编辑:程序博客网 时间:2024/06/05 06:07

不要sd卡 不要数据库 也可以把.txt文件 放在android app的某个地方

Android app的"/data/com.example.demo/xxx.txt"这个文件夹下就可以存放我们想放的文件,并且也可以和其他app共享数据。


1.下面我们来看看怎么写入数据

/**
* 本地文件存储
* @param key自定义文件名
* @param value是要保存的字符串
*/
public static void save(Context context,String key , String value)
{
//创建输出流
FileOutputStream outStream = null;
       try {
        //Context.MODE_WORLD_READABLE(这个模式权限是可以让外部应用读取这个数据)
        outStream = context.openFileOutput(key+".txt", Context.MODE_WORLD_READABLE );
           outStream.write(value.getBytes());
       } catch (FileNotFoundException e) {
           return;
       }
       catch (IOException e){
           return ;
       }finally {
        //关闭流
if( outStream != null)
{
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


运行完之后,在如下位置,找到自己的包名,打开files文件夹 里面就有刚才保存的key.txt文件,数据就在这里面了。




2.保存完之后,来看看如何提取出来使用(应用内读取)


 /**
 * 本地文件读取
 * @param key已保存的文件名
 */
public static String read(Context context,String key)
{
FileInputStream inStream = null;
ByteArrayOutputStream stream = null;
    try {
    inStream = context.openFileInput(key+".txt");
    stream = new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int length=-1;
while((length=inStream.read(buffer))!=-1)   {
            stream.write(buffer,0,length);
        }  
        return stream.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    catch (IOException e){
        return null;
    }
    finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}  

 3.在其它应用读取这个文件


public String read(String packageName,String key)
{
FileInputStream inStream = null;
ByteArrayOutputStream stream = null;
    try {
    Context c = createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
    inStream = c.openFileInput(key+".txt");
    stream = new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int length=-1;
while((length=inStream.read(buffer))!=-1)   {
            stream.write(buffer,0,length);
        }  
        Log.e("TAG", "AboutSoftActivity =="+stream.toString());
        return stream.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    catch (IOException e){
        return null;
    } catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
    finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}  


大致上和本地读取是没区别的,但是参数那里,可以看到第一个参数不是Context ,而是包名(被读取数据所在应用的包名),这个包名是为了在获取数据的应用里创建Context的( Context c = createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);)

这样的话,就可以在一个程序中读取另一个程序存储的数据了。SP存储和这个也是一样的原理

原创粉丝点击