开发者

Android filewriting, reading

开发者 https://www.devze.com 2023-02-15 03:47 出处:网络
I have a dialog with spinners and an OK button. I also have an arraylist that is populated by the user selecting items in the spinners. I want the app to save the arraylist to a file and load it at ev

I have a dialog with spinners and an OK button. I also have an arraylist that is populated by the user selecting items in the spinners. I want the app to save the arraylist to a file and load it at every launch (because without saving the arraylist is always empty at launch).

So here is the code i am using. The saving should be okay, at least i get the Toast message saying Saving OK. This code is the OKbtn listener so when user clicks ok, an item is added to the arraylist and there comes this code:

if (assignArr.size() > 0)
                      {
                        String filename = "file.txt"; 
                        ArrayList<String> report = new ArrayList<String>(); 

                        for (int i=0; i<assignArr.size(); i++)
                        {
                            repor开发者_如何学运维t.add(assignArr.get(i)); 
                        }

                                FileOutputStream fos; 
                                                try { 
                                                        fos = openFileOutput(filename,Context.MODE_PRIVATE); 
                                        ObjectOutputStream out = new ObjectOutputStream(fos); 
                                        out.writeObject(report); 
                                        out.close(); 
                                        Toast.makeText(MainActivity.this, "Saving OK", Toast.LENGTH_LONG).show();
                                                } catch (FileNotFoundException e) { 
                                                        // TODO Auto-generated catch block 
                                                        e.printStackTrace(); 
                                                } catch (IOException e) { 
                                                        // TODO Auto-generated catch block 
                                                        e.printStackTrace(); 
                                                }
                      }  

I put the loading part to the beginning of the code, but i don't think it matters:

words = new ArrayList<String>(50);   
        try { 
        InputStream is = getResources().getAssets().open("file.txt"); 

        BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
        String line; 

        while((line = br.readLine()) != null) 
        { 
              words.add(line); 
        } 
        is.close(); 
        } 
        catch (IOException e) 
        { 
          e.printStackTrace(); 
        } 

        if (words.size() > 0)
        {
            for (int i=0; i<words.size(); i++)
            {
                assignArr.add(words.get(i));
                Toast.makeText(MainActivity.this, "Loading OK", Toast.LENGTH_LONG).show();
            }
        }

In this case i never get the toast message. One problem can be that where is the file created? Where in the emulator and where on the phone? And regarding the phone, is it created on the sd card or the phone memory? I hope this is the right code to save and load an arraylist. What is wrong with the code?

Update: I replaced InputStream is = getResources().getAssets().open("file.txt"); withInputStream is = openFileInput("file.txt"); Now something happens. I write out the result of the saving and the loading into a toast message, which is weird:

  Toast.makeText(MainActivity.this, "Saving OK + " + report, Toast.LENGTH_LONG).show();

Please take a look at it. The words on the bottom are partly hungarian names. So maybe the saving an arraylist to a file is the problem.

Android filewriting, reading


The file you create via openFileOutput is written into your app's private directory, which you can get using Context#getFilesDir() (usually it's /data/data/<your-app-package>/).

This directory has nothing to do with the assets folder.

0

精彩评论

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