Android数据存储之文件 openFileOutput & openFileInput

来源:互联网 发布:中科院数据共享平台 编辑:程序博客网 时间:2024/05/20 07:31

在Context类中提供了抽象方法:

/**     * Open a private file associated with this Context's application package     * for reading.     *     * @param name The name of the file to open; can not contain path     *             separators.     *     * @return The resulting {@link FileInputStream}.     *     * @see #openFileOutput     * @see #fileList     * @see #deleteFile     * @see java.io.FileInputStream#FileInputStream(String)     */    public abstract FileInputStream openFileInput(String name)        throws FileNotFoundException;
/**     * Open a private file associated with this Context's application package     * for writing.  Creates the file if it doesn't already exist.     *     * @param name The name of the file to open; can not contain path     *             separators.     * @param mode Operating mode.  Use 0 or {@link #MODE_PRIVATE} for the     * default operation, {@link #MODE_APPEND} to append to an existing file,     * {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control     * permissions.     *     * @return The resulting {@link FileOutputStream}.     *     * @see #MODE_APPEND     * @see #MODE_PRIVATE     * @see #MODE_WORLD_READABLE     * @see #MODE_WORLD_WRITEABLE     * @see #openFileInput     * @see #fileList     * @see #deleteFile     * @see java.io.FileOutputStream#FileOutputStream(String)     */    public abstract FileOutputStream openFileOutput(String name, int mode)        throws FileNotFoundException;
所以在Activity、Service等类中直接使用这两个方法就可以了。


1.openFileOutput方法将文件保存在/data/data/<package name>/files目录下。

OutputStream os = openFileOutput("zhangmq.txt", Activity.MODE_PRIVATE);  

2.在使用openFileInput方法获得InputStream对象来读取文件中的数据的时候,只需要指定文件名即可。
InputStream is = openFileInput("zhangmq.txt");

3.实例
protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text = (TextView)findViewById(R.id.text);        try {            OutputStream os = openFileOutput("zhangmq.txt", Activity.MODE_PRIVATE);            String str = "zhangmq";            try {                os.write(str.getBytes("utf-8"));//              os.write(str.getBytes());                os.close();            } catch (UnsupportedEncodingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            InputStream is = openFileInput("zhangmq.txt");            byte[] buffer = new byte[1024];            try {                int byteCount = is.read(buffer);//              String bufferStr = buffer.toString();                String bufferStr = new String(buffer,0,byteCount,"utf-8");                text.setText(bufferStr);                is.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                      } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

也算是比较简单的存储了。


0 0
原创粉丝点击