android开发

Android 中的内部存储

2020-04-06  本文已影响0人  android_Pie

Android 中的内部存储

1.直接I/O存储?(/data/data/项目包/files目录)
1)一个对象:(context)
2)两个方法:
a)openFileOutput(文件名,操作模式)
b)openFileInput(文件名)

    //内部存储的位置:/data/data
    //数据为应用程序私有:卸载时一起删除
    public void doWriteInnerStore(){//写数据
        OutputStream out=null;
        try{
        out=openFileOutput("a.txt",//name为文件名
        Context.MODE_PRIVATE);//mode为文件操作模式(私有,追加)
        //Context.MODE_PRIVATE 表示文件为应用程序私有,内容每次是覆盖
        //Context.MODE_APPEND 文件为应用程序私有且可向文件追加内容
        out.write("hello".getBytes());
        }catch(Exception e){
        e.printStackTrace();    
        }finally{
        if(out!=null)try{out.close();}catch(Exception e){}
        }
    }

    public void doReadInnerStore(){//读数据
        InputStream in=null;
        try{
        in=openFileInput("a.txt");
        byte[] buf=new byte[1024];
        int len=in.read(buf);
        Log.i("TAG", new String(buf,0,len));
        }catch(Exception e){
        e.printStackTrace();    
        }finally{
        if(in!=null)try{in.close();}catch(Exception e){}
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读