Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008, 10:22 PM
Member
 
Join Date: Apr 2008
Posts: 2
Stev0 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-12-2008, 11:31 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-13-2008, 05:41 AM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-13-2008, 08:14 AM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
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....
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-14-2008, 11:18 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Hello bobleny,

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

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:

Code:
System.out.println(myarray[0]); System.out.println(myarray[1]); System.out.println(myarray[2]); System.out.println(myarray[3]);
Hope this helps!
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|| Don't forget to:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 04-14-2008 at 11:20 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-17-2008, 04:48 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
YOu can also implement it using Scanner class... "reading contents"

regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-17-2008, 08:18 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Here is a simple way that how to write array elements to a text file

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()); } } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 09:59 PM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 03:34 PM
String byte storage bozovilla New To Java 1 11-24-2007 07:35 AM
j2me.storage.RandomAccessStream certificate error asdfghjkl CLDC and MIDP 1 11-09-2007 04:27 PM
Help with Array susan New To Java 1 08-07-2007 05:32 AM


All times are GMT +3. The time now is 11:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org