Results 1 to 7 of 7

Thread: Array storage

  1. #1
    Stev0 is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default Array storage

    Hello all,
    In class the discussion came up as to saving an array. There is confusion as to whether an array can be saved. The general thought is that an array is an in-memory process only and that the data structure of an array can be saved as a record in a file (binary or ASCII). Can an array be saved directly to a file?
    Thank you for clarifying this elementary concept.
    Steve

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Using the toString() function with arrays you can get a string representation of the array, but i have no idea how to get it back from that point. You can always do it by writing a function to save it in your own format and then parse it back to the array when it comes back. Good Luck.

  3. #3
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    Saving an array as a whole, well you could break the array into a string with spaces then write that into a file. When you want the same array just open the file and combine the string into an array. I hope that make sense.

  4. #4
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    I'm new to Java, so I don't know how to do this, but in PHP, I would copy the array contents to a file.

    For example, if I have an array like this:
    array[0] = "Joe"
    array[1] = "Bill"
    array[2] = "Jessica"
    array[3] = "Amanda"
    array[4] = "Tina"

    ---
    Then the text file would look like this:
    Joe
    Bill
    Jessica
    Amanda
    Tina

    ---
    Then, I simply need to create an array of each line and I'm back to the original array. As of right now, I don't know how to write or read a file....

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    Hello bobleny,

    To read in the contents of a text file and add each line to an array, use this code:

    Java Code:
    FileInputStream in = new FileInputStream("yourFile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    		
    String[] myarray;
    myarray = new String[4];
    		
    for (int i = 0; i < myarray.length; i++){
    myarray[i] = br.readLine();
    }
    		
    in.close();
    Then you can print out the array content to the console using:

    Java Code:
    System.out.println(myarray[0]);
    System.out.println(myarray[1]);
    System.out.println(myarray[2]);
    System.out.println(myarray[3]);
    Hope this helps!
    Last edited by DonCash; 04-14-2008 at 10:20 AM.
    Did this post help you? Please me! :cool:

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    YOu can also implement it using Scanner class... "reading contents"

    regards,
    sukatoa

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Here is a simple way that how to write array elements to a text file

    Java Code:
    public class ArrayToFile {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            String[] numbers = {"One", "Two", "Three", "Four", "Five"};
            int i = 0;
            
            FileWriter fw = null; 
            
            try {
                fw = new FileWriter("test.txt");
                BufferedWriter out = new BufferedWriter(fw);
    
                while(i < numbers.length){
                    out.write(numbers[i]);
                    out.newLine();
                    i++;
                }
                out.close();
            } 
            catch (IOException ex) {
                System.out.println(ex.getLocalizedMessage());
            }
        }
    
    }

Similar Threads

  1. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 08:59 PM
  2. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM
  3. String byte storage
    By bozovilla in forum New To Java
    Replies: 1
    Last Post: 11-24-2007, 06:35 AM
  4. j2me.storage.RandomAccessStream certificate error
    By asdfghjkl in forum CLDC and MIDP
    Replies: 1
    Last Post: 11-09-2007, 03:27 PM
  5. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:32 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
  •