Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:56:55+00:00 2026-06-08T13:56:55+00:00

I have an ArrayList full of strings that the user populated it with to

  • 0

I have an ArrayList full of strings that the user populated it with to use in a different activity in a ListView to view the strings they have saved.
I want the ArrayList they populated to be saved but I am so lost on how to get it to work. I’ve tried FileOutputStream, SharedPreferences. I looked at many examples.

for example i have

ArrayList<String> give = new ArrayList<String>();

and to save the arraylist ive tried stuff like

FileOutputStream fos = openFileOutput(MYFILENAME, Context.MODE_PRIVATE);
fos.write(give.getBytes());
fos.close();

but this does not work at all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:56:57+00:00Added an answer on June 8, 2026 at 1:56 pm

    Here is some code to take a serializable object and write it to file, this maybe what you need. Tested it out with and ArrayList and it works fine. You can also modify the output and instead of writing it to file you can pass it to an activity using extras or its bundle. I used this method for android versions < 3.0.

    To Read a file already containing serialized object:

    String ser = SerializeObject.ReadSettings(act, "myobject.dat");
    if (ser != null && !ser.equalsIgnoreCase("")) {
        Object obj = SerializeObject.stringToObject(ser);
        // Then cast it to your object and 
        if (obj instanceof ArrayList) {
            // Do something
            give = (ArrayList<String>)obj;
        }
    }
    

    To Write an object to file use:

    String ser = SerializeObject.objectToString(give);
    if (ser != null && !ser.equalsIgnoreCase("")) {
        SerializeObject.WriteSettings(act, ser, "myobject.dat");
    } else {
        SerializeObject.WriteSettings(act, "", "myobject.dat");
    }
    

    Class to serialize an object:

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Serializable;
    
    import android.content.Context;
    import android.util.Base64InputStream;
    import android.util.Base64OutputStream;
    import android.util.Log;
    
    /**
     * Take an object and serialize and then save it to preferences
     * @author John Matthews
     *
     */
    public class SerializeObject {
        private final static String TAG = "SerializeObject";
    
        /**
         * Create a String from the Object using Base64 encoding
         * @param object - any Object that is Serializable
         * @return - Base64 encoded string.
         */
        public static String objectToString(Serializable object) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                new ObjectOutputStream(out).writeObject(object);
                byte[] data = out.toByteArray();
                out.close();
    
                out = new ByteArrayOutputStream();
                Base64OutputStream b64 = new Base64OutputStream(out,0);
                b64.write(data);
                b64.close();
                out.close();
    
                return new String(out.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * Creates a generic object that needs to be cast to its proper object
         * from a Base64 ecoded string.
         * 
         * @param encodedObject
         * @return
         */
        public static Object stringToObject(String encodedObject) {
            try {
                return new ObjectInputStream(new Base64InputStream(
                        new ByteArrayInputStream(encodedObject.getBytes()), 0)).readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * Save serialized settings to a file
         * @param context
         * @param data
         */
        public static void WriteSettings(Context context, String data, String filename){ 
            FileOutputStream fOut = null; 
            OutputStreamWriter osw = null;
    
            try{
                fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);       
                osw = new OutputStreamWriter(fOut); 
                osw.write(data); 
                osw.flush(); 
                //Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
            } catch (Exception e) {       
                e.printStackTrace(); 
               // Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
            } 
            finally { 
                try { 
                    if(osw!=null)
                        osw.close();
                    if (fOut != null)
                        fOut.close(); 
                } catch (IOException e) { 
                       e.printStackTrace(); 
                } 
            } 
        }
    
        /**
         * Read data from file and put it into a string
         * @param context
         * @param filename - fully qualified string name
         * @return
         */
        public static String ReadSettings(Context context, String filename){ 
            StringBuffer dataBuffer = new StringBuffer();
            try{
                // open the file for reading
                InputStream instream = context.openFileInput(filename);
                // if file the available for reading
                if (instream != null) {
                    // prepare the file for reading
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
    
                    String newLine;
                    // read every line of the file into the line-variable, on line at the time
                    while (( newLine = buffreader.readLine()) != null) {
                        // do something with the settings from the file
                        dataBuffer.append(newLine);
                    }
                    // close the file again
                    instream.close();
                }
    
            } catch (java.io.FileNotFoundException f) {
                // do something if the myfilename.txt does not exits
                Log.e(TAG, "FileNot Found in ReadSettings filename = " + filename);
                try {
                    context.openFileOutput(filename, Context.MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                Log.e(TAG, "IO Error in ReadSettings filename = " + filename);
            }
    
            return dataBuffer.toString();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList full of strings arrays that I built like this: String[]
I have an arraylist that gets different type of values in it, 1st value->
So let's say I have an ArrayList full of Products that need to be
I have an ArrayList() that I want to display as a Spinner. Problem is,
Ok, so I have an ArrayList (arrBok) , which is full of book objects
I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have two arraylist with a number of model objects.I want to find the
I have an ArrayList of Routine objects and I must use this method to
i have an arraylist of strings in my servlet which im passing forwarding using
I've got an activity that presents a listview of tracks of songs. When an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.