|
|
|
|
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.
|
|

04-12-2008, 10:22 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 2
|
|
|
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, 11:31 PM
|
 |
Member
|
|
Join Date: Apr 2008
Location: State College, PA
Posts: 50
|
|
|
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, 05:41 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 23
|
|
|
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, 08:14 AM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
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, 11:18 AM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
Hello bobleny,
To read in the contents of a text file and add each line to an array, use this 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:
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.
|
|

04-17-2008, 04:48 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
|
|
|
YOu can also implement it using Scanner class... "reading contents"
regards,
sukatoa
|
|

04-17-2008, 08:18 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
|
|
Here is a simple way that how to write array elements to a text file
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)
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
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