Saving arrays to file

Hi,I'm trying to save the contents of an integer array to a text file on disk. Can anyone tell me the best way of doing this please?many thanks.
[173 byte] By [ajeet_20] at [2007-9-26 1:28:45]
# 1
PrintWriter writer = new PrintWriter(new FileWriter("filename"));for(int i=0; i<array.length; i++) {writer.println(array);}writer.close();>
wookash at 2007-6-29 1:14:57 > top of Java-index,Archived Forums,Java Programming...
# 2
thank u!
ajeet_20 at 2007-6-29 1:14:57 > top of Java-index,Archived Forums,Java Programming...
# 3

If you just want to save and read an int[] from a file, have a look at the ObjectOutputStream and ObjectInputStream classes in java.io. But the data will be saved in binary format, not in human readable, which leads us to the question what you mean by a text file. How exactly do you want to save that array?

Example:int[] arrayToFile = {1,4,566,252,0,454,3,-775,4366,-5455,-28};

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream("c:\\array.dat"));

oos.writeObject(arrayToFile);

oos.close();

ObjectInputStream ois = new ObjectInputStream(

new FileInputStream("c:\\array.dat"));

int[] arrayFromFile = (int[]) ois.readObject();

for (int j=0; j < arrayFromFile.length; j++) {

System.out.println(arrayFromFile[j]);

}

ois.close();

jsalonen at 2007-6-29 1:14:57 > top of Java-index,Archived Forums,Java Programming...