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

  • Home
  • SEARCH
  • 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 6189069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:20:40+00:00 2026-05-24T02:20:40+00:00

I’m currently building an application where the user will generate data over time and,

  • 0

I’m currently building an application where the user will generate data over time and, should he/she has an internet connection, transmit it to the web. However, if he doesn’t have web access, I need to store this data in the phone until the user recovers his access, when I’ll need to recover this data to be transmitted. However, I’m facing lots of troubles to do this, as per below.

Note: before anything, I’m using a local java-created file because I know no other way to save/restore this data on the device. If you happen to know any other way to store/access this data from within the device please feel free to comment here.

Just for reference,

  • phantoms is an ArrayList containing objects with the data I need to
    store,
  • Arquivador is the class that I’m using to make my data persistent and to recover it,
  • Funcionario is the class with the data generated by the program (just a few strings and numbers)

I am able to write a file to the file system through the code below, on my Activity:

try {
    arq = new Arquivador();
    arq.addFirstObjectInFile(
            openFileOutput("dados.jlog", MODE_WORLD_WRITEABLE),
            phantoms.get(0));
    phantoms.remove(phantoms.get(0));
    for (Funcionario func : phantoms) {
        arq.addObjectInFile(openFileOutput("dados.jlog", MODE_APPEND),
                func);
    }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
}

Here is the code inside Arquivador that adds the data to a file:

public void addObjectInFile(FileOutputStream arquivo,
        Object objetoAAdicionar) {
    try {
        ObjectOutputStream aoos = new ObjectOutputStream(arquivo);
        aoos.writeObject(objetoAAdicionar);
        aoos.close();
    } catch (IOException ioe) {
        Log.d(TAG_NAME, "Erro no Appendable OOS.");
    }
}

public void addFirstObjectInFile(FileOutputStream arquivo,
        Object objetoAAdicionar) {
    try {
        AppendableObjectOutputStream aoos = new AppendableObjectOutputStream(
                arquivo);
        aoos.writeObject(objetoAAdicionar);
        aoos.close();
    } catch (IOException ioe) {
        Log.d(TAG_NAME, "Erro no Appendable OOS.");
    }
}

You will notice that I’m adding data to persistence in 2 steps, the first Object and the rest of them. This was an idea I saw on this post, here in StackOverflow, to allow appending data to a Java generated file. I have no problem with this code, it works perfectly.

Later on, back on my Activity, the internet connection is detected and I try to recover the file saved on the disk:

phantoms = new ArrayList<Funcionario>();
Object obj = arq.readObjectFromFile(openFileInput("dados.jlog"));
Funcionario func = null;
if (obj instanceof Funcionario) {
    func = (Funcionario) obj;
}
while (func != null) {
    phantoms.add(func);
    arq.removeObjectFromFile(openFileInput("dados.jlog"), func,
            getApplicationContext());
    func = (Funcionario) arq
            .readObjectFromFile(openFileInput("dados.jlog"));
}

The original idea was to read 1 object at a time, then attempt to transmit it and, if successful, erase the object from the file (so it didn’t get retransmitted). However, I was having too many error messages with this. Instead, I decided to load all the objects at once, one by one, to see where my problem was more clearly.

Back to the Arquivador class:

public Object readObjectFromFile(FileInputStream arquivo) {
    Object retorno = null;
    if (arquivo.equals(null)) {
        Log.e(TAG_NAME, "FIS is null!");
    }
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(arquivo);
        retorno = ois.readObject();
    } catch (IOException ioex) {
    } catch (ClassNotFoundException e) {
    } finally {
        try {
            if (ois != null) ois.close();
        } catch (IOException e) {
        }
    }
    return retorno;
}

public void removeObjectFromFile(FileInputStream arqPrincipal,
        Object objetoARemover, Context contexto) {
    try {
        // Construct the new file that will later be renamed to the original
        // filename.
        ObjectOutputStream oos = new ObjectOutputStream(
                contexto.openFileOutput("dados.jlog.temp",
                        contexto.MODE_APPEND));
        ObjectInputStream ois = new ObjectInputStream(arqPrincipal);
        Object obj = null;
        // Read from the original file and write to the new
        // unless content matches data to be removed.
        try {
            while ((obj = ois.readObject()) != null) {
                if (!(objetoARemover.equals(obj))) {
                    oos.writeObject(obj);
                    oos.flush();
                }
            }
        } catch (EOFException eof) {
        } finally {
            oos.close();
            ois.close();
            // Delete the original file
            File aDeletar = contexto.getFileStreamPath("dados.jlog");
            File aRenomear = contexto.getFileStreamPath("dados.jlog.tmp");
            if (!aDeletar.delete()) {
                return;
            } else {
                // Rename the new file to the filename the original file
                // had.
                if (!aRenomear.renameTo(aDeletar)) Log.d(TAG_NAME,
                        "Error renaming file");
                else Log.d(TAG_NAME, "Renaming successful");
            }
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        Log.d(TAG_NAME, "Arquivo não encontrado");
    } catch (IOException ex) {
        ex.printStackTrace();
        Log.d(TAG_NAME, "Erro de entrada/saída");
    } catch (ClassNotFoundException e) {
        Log.d(TAG_NAME, "Classe Não Encontrada.");
    }
}

The method readObjectFromFile() seems to work just fine. I can even convert the read Object to Funcionario class and read its data.

My problems appear when I use removeObjectFromFile(). The idea is to create a temporary file to store objects from “dados.jlog” file other than the one that has been already loaded in the main program, then once this temp file is created the file “dados.jlog” should be deleted and the temporary file should be renamed to replace it.

The first thing I found out to be strange here is that the ois.readobject() keeps throwing an EOFException. While this makes sense, the tutorial I read on the internet doesn’t mention this error. In fact, their code indicates that when the readObject() method reaches the EOF, it would return a reference to null, but instead this class throws this EOFException. I handled this exception in the code – though I’m not sure if this would be the right way to do it.

Another thing I find strange is the fact that this code fails to recognize the object that it should NOT copy. When I compare the object read from the file to the one received as argument, no matter what I try ( == , equals(), etc) they seem different objects to the compiler. Funcionario class is serializable has a serialversionUID, so the object read from the file should be identical to the one I stored. Worse than this, these 2 Objects being compared are read from the same file. They should be identical, right?

After creating the temporary file, I try to delete the original file and rename the temporary file. Though this seems to be working, once the removeObjectFromFile() ends the first time, the program is unable to read the data from the file “dados.jlog” again. I can’t read the remaining data from the file and the program enters on an endless loop – since the 1st object is never removed from the list in the file.

Please enlighten me with this matter.

  • 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-05-24T02:20:41+00:00Added an answer on May 24, 2026 at 2:20 am

    Personally I’d use an SQLLite database. Store each object in a row in the database. Once you’ve successfully transmitted you can remove the row from the database.

    You can even reuse most of your code that you’ve already done. The easiest way to get there from where you are is to use a separate file for each object and store only the filename of the object in the database. You can then iterate over the rows in the database. Each time you transmit an object to your server simply delete that row from the database (and remove the file from the filesystem!). No rows in the database means no objects remain to be transmitted.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.