Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

saving an integer array into a .txt file

807588Jun 4 2009 — edited Jun 5 2009
hii, im only a beginner in java, so this is the only code i've learned so far for saving array elements into a file

public static void saveNames(String [] name) throws IOException
{
FileWriter file = new FileWriter("Example\\names.txt");
BufferedWriter out = new BufferedWriter(file);

int i = 0;

while (i < name.length)
{
if (name[i] != null)
{
out.write(name);
out.newLine();
}
i++;

}
out.flush();
out.close();
}

However, this is only used for string arrays; and i can't call the same method for an integer array (because it's not compatible with null)
I don't really understand this code either... since my teacher actually just gave us the code to use, and didn't really explain how it works

I have the same problem for my reading a file code as well --> it's only compatible with string

public static String [] readNames (String [] name) throws IOException
{
int x = 0;
int counter = 0;
String temp = "asdf";
name = new String [100];

FileReader file = new FileReader("Example\\names.txt");
BufferedReader in = new BufferedReader(file);

int i = 0;

while (i < array.length && temp != null) // while the end of file is not reached
{
temp = in.readLine();

if (temp != null)
{
name[i] = temp; // saves temp into the array
}
else
{
name[i] = "";
}
i++;
}

in.close(); // close the file

return name;
}

Could someone suggest a way to save elements from an integer array to a file, and read integer elements of that file?
Or if it's possible, replace null with a similar variable (function?), that is suitable for the integer array (i can't use temp != 0 because some of my elements might be 0)

Thanks so much!!!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 3 2009
Added on Jun 4 2009
3 comments
371 views