开发者

Using the Internal Storage

In my app I have a Linked List (type List), every time the user starts the app I want to restore the list from the Internal Storage (if there is a saved file on the Internal Storage) or create a new list (to be saved later on).

How can I use the开发者_StackOverflow中文版 read/write functions (on FileInputStream/FileOutputStream) to do it ?


refer this Question , it's about how to Save an Object by using SharedPreferences : how android SharedPreferences save/store object ?

EDIT : refer this one , its is about how to save / restore object using internal storage


//save file into internal 
try {
                    URL url = new URL("your url");
                     java.io.BufferedInputStream in = new java.io.BufferedInputStream(url.openStream());

                       FileOutputStream fos ;
                       fos = openFileOutput("test11.xml",Context.MODE_WORLD_WRITEABLE);
                        java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                        byte[] data = new byte[1024];
                        int x=0;
                        while((x=in.read(data,0,1024))>=0){
                            bout.write(data,0,x);               
                        }
                        fos.flush();
                        bout.flush();
                        fos.close();
                        bout.close();
                        in.close();

                    fos.close();

                    Toast.makeText(
                            AndroidInternalStorageActivity.this, 
                            fileName + " saved", 
                            Toast.LENGTH_LONG).show();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

//List of internal files                
 void ShowSavedFiles(){
        SavedFiles = getApplicationContext().fileList();
        Log.e("file path is :",Arrays.toString(SavedFiles));
        ArrayAdapter<String> adapter
        = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                SavedFiles);

        listSavedFiles.setAdapter(adapter);
    }

//Delete internal file 

    File dir = getFilesDir();
        File file = new File(dir, "test.xml");
        Log.e("file path : ",file.toString());
        boolean deleted = file.delete();
        if(deleted)
        {
            Log.e("delete ","true");
        }
        else
        {
            Log.e("delete","false");
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜