Results 1 to 2 of 2
  1. #1
    kingkongjaffa is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default Help with object arrays

    Hi, I have managed to gather user input using the java.util.Scanner and during runtime this input is stored in the properties of a class Called customer,

    how do I set up an array of customer objects

    and then save them to a txt file ( this save location will be changed to a database in future. )

    basicly I would like to know how to save multiple customers information in a .txt file, by adding the class(object?) to an array and then saving the array to a .txt file.

    and how to read it back to be able to print the contents of the txt file to the commandline.

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    I would use an ArrayList to store Customer objects

    //Something like this. This assumes that you followed convention, and 'customer' is actually 'Customer'
    Java Code:
    ArrayList<Customer> yourVariable = new ArrayList<Customer>();
    Two options to make the data persistent:

    1. Serialize the object and let Java do the heavy lifting for you.

    JDK 5.0 Serialization-related APIs & Developer Guides -- from Sun Microsystems

    or

    2. Create a text file with all the data you will need to recreate the objects. When you read the file you will have to recreate the objects with the data you saved in the file. You will have to think of a structure that you will be able to consistently parse when you save the file.
    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    Last edited by yellowledbet; 08-04-2011 at 04:32 PM.

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  2. Object Orientation - Arrays
    By Boysie in forum Jobs Discussion
    Replies: 4
    Last Post: 04-14-2010, 09:32 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Object Arrays
    By rsvr in forum New To Java
    Replies: 9
    Last Post: 12-05-2009, 11:27 PM
  5. Replies: 2
    Last Post: 12-10-2008, 04:40 PM

Posting Permissions

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