Results 1 to 4 of 4
  1. #1
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Are there alternate approaches?

    I have a class that includes 3 data fields. The class stores instances of the class in an ArrayList.
    I need to write code to save the information in the arraylist to a disk file, and then read it back when I reopen the program.

    The approach for doing this I am pursuing is to convert the field data into character strings, concatenate them into comma separated fields and separate each instance with a newline.
    That means converting double values to character strings and then converting back to double values upon read. Is there another way to store the information, or must everything serialized to a disk file be saved and read as a character? I have not found File Read functions that return anything other than character values.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Are there alternate approaches?

    You could also save them as xml or as serialized objects.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,871
    Rep Power
    5

    Default Re: Are there alternate approaches?

    I once had a requirement where I needed to store the state of java Swing components to a file; I remember using XMLEncoder in the past to do that without having to write piles of code. You should be able to do the same for your far simpler situation.

    XMLEncoder (Java Platform SE 7 )

    And of course XMLDecoder to load it back again. More up to date, you could investigate using a JSON conversion API to store the data as JSON data in stead of verbose XML.

    Jackson JSON Processor - Home
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Are there alternate approaches?

    Quote Originally Posted by ras_oscar View Post
    I have not found File Read functions that return anything other than character values.
    Recommended reading: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    Plenty of ways to read in non-character files.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Java HangMan - Alternate between players
    By JohnyQ in forum Networking
    Replies: 1
    Last Post: 11-22-2011, 02:45 AM
  2. memory saving alternate to arrays
    By boys21 in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 12:20 PM
  3. Alternate Algorithm? Can you think of one?
    By VinceGuad in forum New To Java
    Replies: 8
    Last Post: 03-18-2009, 04:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •