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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:19:49+00:00 2026-06-16T04:19:49+00:00

I have some problems when trying to deserialize a list of objects. I have

  • 0

I have some problems when trying to deserialize a list of objects. I have searialized a list of objects into a .dat file. If I want to retrieve the data from that file later, when I try to deserialize that list I don’t get the desired result. Here is the code.

Serialization:

MyFile mf = new MyFile("2012-12-18.dat");
             mf.Open();
             FileOutputStream fos = mf.GetOS();

             Iterator<Element> currencies = cube.select("Rate").iterator();
             ISerializare[] lista = new ISerializare[31];
             int i=0;
             while (currencies.hasNext()){
                    MyCurrency newCurrency=new MyCurrency();
                    Element newElement=currencies.next();
                    newCurrency.setSymbol(newElement.attr("currency"));
                    newCurrency.setValue(Double.parseDouble(newElement.text()));
                    lista[i] = newCurrency;
                    System.out.println(newCurrency.toString());
                    i++;
             }
             DataOutputStream dos = new DataOutputStream(fos);

             for(int j=0;j<i;j++){
                 lista[j].ObjectSerialization(dos);
             }
             dos.close();
public class MyFile {

    File fisier;
    String name;

    public MyFile(String n){
        name = n;
    }

    public void Open(){
        fisier = new File(name);
    }

    public FileOutputStream GetOS() throws IOException{
        return new FileOutputStream(fisier);
    }

    public FileInputStream GetIS() throws IOException{
        return new FileInputStream(fisier);
    }
}



MyFile mf = new MyFile("2012-12-18.dat");
     mf.Open();    
     FileInputStream fis = mf.GetIS();
     DataInputStream dis = new DataInputStream(fis);

     for(ISerialization element:list){
         element.ObjectDeserialization(dis);
         System.out.println(element.toString());

and here is MyCurency class:

    public class MyCurrency implements ISerialization
{
private String symbol;
private double value;
public String getSymbol() {
    return symbol;
}
public void setSymbol(String symbol) {
    this.symbol = symbol;
}
public double getValue() {
    return value;
}
public void setValue(double value) {
    this.value = value;
}
public String toString(){
    return symbol +" = "+value + " RON";
}
@Override
public void ObjectSerialization(DataOutputStream dos) throws IOException {
    dos.writeDouble(value);
}
@Override
public void ObjectDeserialization(DataInputStream dis) throws IOException {
    value = dis.readDouble();
}

Can you please tell me what is wrong?

  • 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-16T04:19:50+00:00Added an answer on June 16, 2026 at 4:19 am

    Can you please tell me what is wrong?

    There are lots of things which could be wrong. Since you are not specific I will have to use my imagination.

    The method names don’t follow Java Coding Conventions which doesn’t make them easy to read. Using a code formatter would help.

    The most obvious issue is that you only write the value field which means the symbol will be null after you deserialize it.

    Also

    System.out.println(element.toString());
    

    is the same as

    System.out.println(element);
    

    And

    return symbol +" = "+value + " RON";
    

    has no formatting for value so it might print YEN 100.0 or USD 100.0 when it should be YEN 100 and USD 100.00 and it’s not obvious why you have ” RON” at the end.


    If it helps at all, this is how I would write it

    Collection<Element> currencies = cube.select("Rate");
    
    // write out
    File mf = new File("2012-12-18.dat");
    DataOutputStream dos = new DataOutputStream(
            new BufferedOutputStream(new FileOutputStream(mf)));
    dos.writeInt(currencies.size()); // so you know how many to read.
    for (Element currency : currencies) {
        MyMoney newCurrency = new MyMoney(currency);
        newCurrency.writeTo(dos);
    }
    dos.close();
    
    // read in
    DataInputStream dis = new DataInputStream(
            new BufferedInputStream(new FileInputStream(mf)));
    int count = dis.readInt();
    List<MyMoney> myMoneys = new ArrayList<>();
    for (int i = 0; i < count; i++)
        myMoneys.add(new MyMoney(dis));
    dis.close();
    
    public class MyMoney {
        private final String symbol; // this is a currency
        private final BigDecimal value;
    
        public MyMoney(Element element) {
            this(element.attr("currency"), new BigDecimal(element.text()));
        }
    
        public MyMoney(DataInputStream dis) throws IOException {
            symbol = dis.readUTF();
            value = new BigDecimal(dis.readUTF());
        }
    
        public MyMoney(String symbol, BigDecimal value) {
            this.symbol = symbol;
            this.value = value;
        }
    
        public String getSymbol() {
            return symbol;
        }
    
        public BigDecimal getValue() {
            return value;
        }
    
        public String toString() {
            return symbol + " " + value;
        }
    
        public void writeTo(DataOutputStream dos) throws IOException {
            dos.writeUTF(symbol);
            dos.writeUTF(value.toString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to learn Erlang and have been running into some problems
I have some problems when trying to get multiple fields from a jQuery form.
I'm trying to dig into asp.net and C# and have some problems; I've built
im having some problems trying to read a SQlite data base from flash using
I'm having some problems trying to perform a query. I have two tables, one
I'm trying to build firefox but I'm having some problems. I currently have Visual
I have this query, which is giving me some problems... I am trying to
I have some problems when trying to pass some parameters to PHP via JS.
I've run into some problems while trying to write a smallshell in c. The
I am practicing with PHP and AJAX but I have some problems! I'm trying

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.