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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:51:38+00:00 2026-06-14T11:51:38+00:00

I have map collection like this: Map<File,Boolean> status = new HashMap<File,Boolean>(); Wrote it to

  • 0

I have map collection like this:

Map<File,Boolean> status = new HashMap<File,Boolean>();

Wrote it to file using serializable then i must edit collection(status) by adding new element
or remove one or more element.

Serializable edit (write changed thing just) to file by add and remove element or empties it and fill it again as object?
and how (if i can) to add or remove elements from file??

for any thing not clear ask me plz

  • 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-14T11:51:39+00:00Added an answer on June 14, 2026 at 11:51 am

    It is not possible remove only the part of file. Every time when You make changes in the map, You must to overwrite the whole file. See my example:

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.Map;
    
    public class SerializationProgram {
    
        public static void main(String[] args) throws Exception {
            emptyTest();
            addAndRemoveTest();
        }
    
        private static void emptyTest() throws Exception {
            ObjectSerializer serializer = new ObjectSerializer();
            serializer.serialize(new FileStatus());
            FileStatus persisted = serializer.deserialize(FileStatus.class);
            test(persisted, new FileStatus());
        }
    
        private static void addAndRemoveTest() throws Exception {
            ObjectSerializer serializer = new ObjectSerializer();
            FileStatus toPersist = new FileStatus();
            FileStatus mirror = new FileStatus();
    
            toPersist = serializeDeserializeAndTest(serializer, toPersist, mirror);
    
            File file = new File("/test/test.txt");
            Boolean status = Boolean.TRUE;
    
            // Add file status
            toPersist.addOrChangeStatus(file, status);
            mirror.addOrChangeStatus(file, status);
    
            toPersist = serializeDeserializeAndTest(serializer, toPersist, mirror);
    
            // remove file status
            toPersist.removeStatus(file);
            mirror.removeStatus(file);
    
            toPersist = serializeDeserializeAndTest(serializer, toPersist, mirror);
            toPersist = serializeDeserializeAndTest(serializer, toPersist, mirror);
        }
    
        private static FileStatus serializeDeserializeAndTest(
                ObjectSerializer serializer, FileStatus toPersist, FileStatus mirror)
                throws Exception {
            serializer.serialize(toPersist);
            toPersist = serializer.deserialize(FileStatus.class);
            test(toPersist, mirror);
            return toPersist;
        }
    
        private static void test(FileStatus given, FileStatus expected) {
            if (given.equals(expected)) {
                System.out.println("everything is OK");
            } else {
                throw new IllegalArgumentException("Are not the same!");
            }
        }
    }
    
    class ObjectSerializer {
    
        private static final boolean OVERWRITE_MODE = false;
    
        public <T> void serialize(T instance) throws IOException {
            OutputStream overwriteStream = new FileOutputStream(getFile(instance.getClass()), OVERWRITE_MODE);
            OutputStream bufferedStream = new BufferedOutputStream(overwriteStream);
            ObjectOutputStream objectstream = new ObjectOutputStream(bufferedStream);
            objectstream.writeObject(instance);
            objectstream.close();
        }
    
        @SuppressWarnings("unchecked")
        public <T> T deserialize(Class<T> clazz) throws Exception {
            InputStream input = new BufferedInputStream(new FileInputStream(getFile(clazz)));
            ObjectInputStream objectStream = new ObjectInputStream(input);
            T instance = (T) objectStream.readObject();
            objectStream.close();
            return instance;
        }
    
        private <T> File getFile(Class<T> clazz) {
            return new File(clazz.getName());
        }
    }
    
    class FileStatus implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        Map<File, Boolean> statusMap = new HashMap<File, Boolean>();
    
        public void addOrChangeStatus(File file, boolean status) {
            statusMap.put(file, status);
        }
    
        public void removeStatus(File file) {
            statusMap.remove(file);
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((statusMap == null) ? 0 : statusMap.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            FileStatus other = (FileStatus) obj;
            if (statusMap == null) {
                if (other.statusMap != null)
                    return false;
            } else if (!statusMap.equals(other.statusMap))
                return false;
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable like: Collection<Map<String, String>> allFieldValues; In this variable I have all
I'm have a map that looks like this: Map[ A -> Collection[B]] . This
I have a collection of objects in JavaScript like this: Object collection = new
If I have a Map like this: HashMap<Integer, ComparableObject> map; and I want to
I have a Json file that looks like this: [ { field:val }, ....
My program basically looks like this: using(XmlReader reader = XmlReader.Create(File.Open(path, FileMode.Open, FileAccess.Reader, FileShare.Read)) {
I have this a mongo collection like this: { _id : ObjectId(4fd930ecd729f5b31ea5ad03), O_ORDERKEY :
I have a map reduce like this: map: function() { emit(this.username, {sent:this.sent, received:this.received}); }
I have a table structure something like this table Collection Id Name table Product
If I have code that looks like this: :collection => { :download_print_use => :get

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.