This discussion is archived
6 Replies Latest reply: Nov 11, 2010 7:25 AM by JoachimSauer RSS

Read data from file into ArrayList

Moon123 Newbie
Currently Being Moderated
Hi there,
I have a class called Product that describes the charactristics of products such as name, id, price etc. I have all the set and get methods in my Product class. I store the information about my products into an ArrayList. I then write the information about my products in a file.
so far so good but...

I now want to read back the data from the file and store it in my ArrayList again. I know that I can use Parsing and read in data into an ArrayList as String, int, double etc but is there anyway to read it in as of "Product" (then it would belong to my Product class)?

Thank you in advance.
  • 1. Re: Read data from file into ArrayList
    DarrylBurke Guru Moderator
    Currently Being Moderated
    1. Are you aware of serialization?
    2. Get a name, I don't like dealing with numbers.

    db
  • 2. Re: Read data from file into ArrayList
    Moon123 Newbie
    Currently Being Moderated
    oh, yes. I forgot about serialization. Just wondering though...is serialization the only option?

    As for the name, I just joined Oracle forum and didn't get a chance to change it. I had a Sun id before. I will look to see how I can change the number to just a name.
    Thanks.
  • 3. Re: Read data from file into ArrayList
    JoachimSauer Journeyer
    Currently Being Moderated
    808072 wrote:
    oh, yes. I forgot about serialization. Just wondering though...is serialization the only option?
    No, of course not.

    There are tons of different approaches. You could use JAXB (or any other XML mapping framework) to serialize to XML.
    You could write to a CSV file (with or wthout library support).
    You could construct your own binary file format.
    You could write an excel sheet with the data in it.
    You could ...

    Such an open ended question doesn't really lead anywhere, you need to be more specific with your requirements.
  • 4. Re: Read data from file into ArrayList
    Moon123 Newbie
    Currently Being Moderated
    Thank you for your response.
    The reason my question was open-ended as you mentioned is that I am trying to find an easy way to explain this to someone whose knowledge of Java is very little. I thought there might be an easier way but I guess serialization would be the easist in this case.

    Thanks a lot for you help again.
  • 5. Re: Read data from file into ArrayList
    JoachimSauer Journeyer
    Currently Being Moderated
    808072 wrote:
    The reason my question was open-ended as you mentioned is that I am trying to find an easy way to explain this to someone whose knowledge of Java is very little.
    Wouldn't a better approach be to ask for an easy-to-explain solution instead? This way we can provide information tailored towards the actual problem you're facing instead of guessing ourselves.

    For an easy-to-explain solution I would actually go with a very simple text-based solution (probably a CSV file).

    The main reason is that it's reasonably easy to write and that the resulting file can easily be inspected and manipulated with a simple text editor. Serialization looks more like "magic" to someone with limited knowledge.
  • 6. Re: Read data from file into ArrayList
    Moon123 Newbie
    Currently Being Moderated
    Ok. You are right. I will do that and hopefully it would be easier for him.
    Thanks for the suggestion.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points