Results 1 to 7 of 7
Thread: Array storage
- 04-12-2008, 09:22 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
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
- 04-12-2008, 10:31 PM #2
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.
- 04-13-2008, 04:41 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
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.
- 04-13-2008, 07:14 AM #4
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....
- 04-14-2008, 10:18 AM #5
Hello bobleny,
To read in the contents of a text file and add each line to an array, use this code:
Then you can print out the array content to the console using: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();
Hope this helps!Java Code:System.out.println(myarray[0]); System.out.println(myarray[1]); System.out.println(myarray[2]); System.out.println(myarray[3]);
Last edited by DonCash; 04-14-2008 at 10:20 AM.
Did this post help you? Please
me! :cool:
- 04-17-2008, 03:48 AM #6
YOu can also implement it using Scanner class... "reading contents"
regards,
sukatoa
- 04-17-2008, 07:18 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
can anyone help... 2d Array
By Mark1989 in forum New To JavaReplies: 2Last Post: 03-12-2008, 08:59 PM -
Would appreciate your help with 2d Array..
By cloudkicker in forum New To JavaReplies: 1Last Post: 02-11-2008, 02:34 PM -
String byte storage
By bozovilla in forum New To JavaReplies: 1Last Post: 11-24-2007, 06:35 AM -
j2me.storage.RandomAccessStream certificate error
By asdfghjkl in forum CLDC and MIDPReplies: 1Last Post: 11-09-2007, 03:27 PM -
Help with Array
By susan in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks