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 9152999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:11:13+00:00 2026-06-17T12:11:13+00:00

When my application starts it connects to a database and downloads some data in

  • 0

When my application starts it connects to a database and downloads some data in XML format. This is the layout:

<Customers>
  <Customer> 
    <name>...</name>
    ...
  </Customer>
   <Customer> 
    <name>...</name>
    ...
  </Customer>
  ...
</Customer>

For each Customer I create an Object of the class Customer wicht I store in a ArrayList. The users can edit and add some data of each Customer and they can upload it back to the server.

The problem is that I don’t know what’s the best way to store the data locally, so if my application closes or the user don’t have internet anymore they’ll have a backup. I now covert all Customer objects back to XML and then store it on the SD Card, but this isn’t a good solution I think. Each customer has about 40 String, 10 integers and a few booleans I have to store. Is there a better way to store the data locally?

I found some code wich can store an ArrayList but it does not work with custom classes.

Edit: I used this tutorial to fix my problem.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.os.Environment;

public class SerializeData {
    public static byte[] serializeObject(Object o) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            ObjectOutput out = new ObjectOutputStream(bos);
            out.writeObject(o);
            out.close();

            // Get the bytes of the serialized object
            byte[] buf = bos.toByteArray();

            return buf;
        } catch (IOException ioe) {
            CreateLog.addToLog(ioe.toString());
            return null;
        }
    }

    public static Object deserializeObject(byte[] b) {
        try {
            ObjectInputStream in = new ObjectInputStream(
                    new ByteArrayInputStream(b));
            Object object = in.readObject();
            in.close();

            return object;
        } catch (ClassNotFoundException cnfe) {
            CreateLog.addToLog(cnfe.toString());

            return null;
        } catch (IOException ioe) {
            CreateLog.addToLog(ioe.toString());

            return null;
        }
    }

    public static void saveData(){
        byte[] arrayData = SerializeData
                .serializeObject(MyTasks.allCustomers);

        BufferedOutputStream bos;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(
                    Environment.getExternalStorageDirectory()
                            + "/myprogram/myarray.txt"));
            bos.write(arrayData);
            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            CreateLog.addToLog(e.toString());
        } catch (IOException e) {
            CreateLog.addToLog(e.toString());
        }
    }

    public static ArrayList<Customer> getData(){
        File afile = new File(Environment.getExternalStorageDirectory()
                + "/myprogram/myarray.txt");
        int size = (int) afile.length();
        byte[] bytes = new byte[size];
        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(afile));
            buf.read(bytes, 0, bytes.length);
            buf.close();
        } catch (FileNotFoundException e) {
            CreateLog.addToLog(e.toString());
        } catch (IOException e) {
            CreateLog.addToLog(e.toString());
        }

        return (ArrayList<Customer>) SerializeData.deserializeObject(bytes); 

    }
}
  • 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-17T12:11:14+00:00Added an answer on June 17, 2026 at 12:11 pm

    As I understand, the Customer structure will be read and written only locally and also by the same or highly related program. For such situation, I think, Java serialization is appropriate as it adds very little code.

    Make the Customer to implement Serializable (put immediately serialVersionUID so you could control versions later). ArrayList is already Serializable. Obtain the file name as explained here and use ObjectInput/Output stream to serialize. You can always migrate to XML/JSON later if the need arises. A simple “hello world” example for serialization can be found here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When a JSF/XPages application starts it reads the faces-config.xml for managed beans, validators etc.
It seems that when a WPF application starts, nothing has focus. This is really
I'm compressing some textures using ETC1 when my application starts. I also have a
Is there any Spring 3 feature to execute some methods when the application starts
I am creating C# winforms application, which connects to the Database. Because I have
I have a mysql database and the application which connects to it, dumps required
I got into this not very good situation.. When web application starts - I
When our application starts up it can't seem to connect to the database (using
I've written a Autoit script that starts a GUI application, when the application starts
I have couple resource DLLs that I currently load when application starts using following

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.