0

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 read/write functions (on FileInputStream/FileOutputStream) to do it ?

Mat
  • 202,337
  • 40
  • 393
  • 406
Belgi
  • 14,542
  • 22
  • 58
  • 68
  • :i think that you have already post a similar question about this : save/restore data ==> http://stackoverflow.com/questions/7665339/android-how-to-save-restore-data/7665408#7665408 – Houcine Oct 06 '11 at 15:16
  • @Houcine it's different, I read the doc on the developers guide as suggested in the previews post but the actual question is how to use those read/write functions. for example read returns an int, how can I use it to restore a saved List... – Belgi Oct 06 '11 at 15:20
  • @Belgi hello again. Following you from Q to Q, it _seems_ that what you're really interested in is called `SharedPreferences` – Laurent' Oct 06 '11 at 15:25
  • refer this question and tell me if it helps : http://stackoverflow.com/questions/4519820/how-to-save-arraylist-in-an-bundle-object – Houcine Oct 06 '11 at 15:27
  • 1
    @Laurent'- isn't SharedPreferences only works for primitive data ? (and thanks for the help!) – Belgi Oct 06 '11 at 15:28
  • @Laurent'- I also checked out the link, I think that the holder won't work after the app closes, that why I want to save the data to the internal storage. – Belgi Oct 06 '11 at 15:32
  • @Belgi : did you see the link of my answer about using SharedPreferences to save an Object ?? – Houcine Oct 06 '11 at 15:34
  • @Houcine - sorry I missed tag, I answerd in the previews comment : "I think that the holder won't work after the app closes, that why I want to save the data to the internal storage" – Belgi Oct 06 '11 at 15:38
  • @Belgi : lol i saw it , no problem :). know you want to save your Object in the internat storage and restore it later ? – Houcine Oct 06 '11 at 15:40

2 Answers2

0
//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");
        }
iAndroid
  • 951
  • 7
  • 18
0

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

Community
  • 1
  • 1
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • I don't see how that post helps : it's about transfering object from one activity another... – Belgi Oct 06 '11 at 15:37