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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:41:28+00:00 2026-05-27T06:41:28+00:00

I am having trouble figuring out one implementation problem, I have one class, it

  • 0

I am having trouble figuring out one implementation problem, I have one class, it behaves like list but instead of holding a file in some collection it saves them on a disk.

The problem occurs when I want to add some element to my list. At the start of my file I have one int that tells me how many objects there are in my list, but I can’t figure out elegant way to update this value. I have something like this:

   public boolean add(T element)
{
    try 
    {
        out.writeObject(element);
        out.flush();
        //and here we need to update the int in my file
    } catch (IOException e) 
    {
        e.printStackTrace();
    } 
    return true;
}

I tried to use something like this:

ObjectOutputStream upd=new ObjectOutputStream(new FileOutputStream(data.getAbsolutePath(),true));

but as I observed it writes some data to the start of the file, some serialization header or sth, how can I update single entry in my file or how to change

ObjectOutputStream

“pointer” to write at the beginning of the file?

  • 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-27T06:41:29+00:00Added an answer on May 27, 2026 at 6:41 am

    Typically with stream based classes (especially higher order streams like OOS), you should rewrite the whole file, anytime you update it.

    If you really INSIST on only updating part of a file, then you should think of the file as made up of N streams, where each ‘stream’ represents one object that you are writing. So i would use a RandomAccessFile for the base file, and then when i want to write an object i would wrap an ObjectOutputStream on top of a ByteArrayOutputStream, write your object into that, then take those bytes, and rewrite those bytes into the RandomAcessFile where you want.

    This probably won’t be particularly efficient, as you will write N OOS headers, and N class descriptions for the object you are writing.

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

Sidebar

Related Questions

I am really having a trouble figuring this one out. I have a table
Having trouble figuring this one out.. I currently have a frmMain and a frmLoading..
I'm having trouble figuring this one out. In my view I have a dropdownlist
I'm having trouble figuring out why the RSS feed appears blank for one page
I'm having trouble figuring out how best to model my data. I have the
I'm having trouble figuring out how to split a string into groups of like
I am having trouble figuring this one out. I need to be able to
I'm having trouble figuring this one out. The situation is no difficult to imagine
We are looking at adopting Git for our SCM but having trouble figuring out
I'm trying to setup a particular workflow but having trouble figuring out how(or if)

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.