Results 1 to 5 of 5
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default designing a save system

    Hi,
    So, I'm working on a little program that creates an array and then executes various algorithms on it. What I want to do though, is save the array in a .txt file so that I don't have to constantly re-enter the values after each run through the program.

    This in my mind requires two classes:

    1) to save the array

    2) to load the array from a .txt file


    Now, I have three questions:

    1) Is this save/load model a good idea, or is there a more efficient way of doing this?

    2) how do you convert the array to a string to print it, or how do you print an array to a text file? I'm guessing this involves some "for" loops that take each array data and then convert to string which is then written.

    3) what would be the most efficient way to load an array? again I'm guessing "for" loops for each line and then assign's them as array values.

    I guess this thread is more of a, making sure I get my design right before I spend hours working on it and then it doesn't work.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Normally if you want your data to persist you would use a database. However writing out to a flat file for something simple is fine.

    You can either iterate over your array and write the data to a File using a FileWriter. Or you can use an ObjectOutputStream to write the entire array. Note the "save" file will not be human readable. So it will depend upon if you want to be able to read and modify the file. Use an ObjectInputStream to read the array back into your Java app.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Is it going to be gui based? I would just have action listeners to read/write.

    1.) You could just use a method to load, and a method to save.

    2.)What does the array contain? numbers? If so, it's easy to convert to strings. A for loop is the correct way to write the array to a text file, since you know how many items are in the array you can use for loops and a file stream and a print writer chained together.

    3.)For loading the array you would probably use a while loop(although a for loop is fine too) and just read from the file storing each item in the correct array spot.

    With saving/loading it will absolutely take some playing around with to get it right. Give it a shot and see how it works.

  4. #4
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    @junkie I'll look into those, since no the file does not need to be edited.

    @sunde887, thanks for your reply, no this wont be gui based just good ole JVM in Eclipse and your right these should be two methods not classes. thanks :)

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your welcome. Whenever I am working on save load stuff it takes me a decent amount of trial and error(mainly since I'm inexperienced with it), finding which readers and writers and how to get the saves and loads working correctly.

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2011, 05:15 AM
  2. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  3. Save the output from system.out.println into a file
    By Iskatel in forum Advanced Java
    Replies: 5
    Last Post: 10-05-2010, 02:52 PM
  4. Help designing a program
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 02:31 AM
  5. Need help for coding and designing
    By g.ganiraju in forum New To Java
    Replies: 11
    Last Post: 11-01-2008, 04:49 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
  •