Thu Feb 07, 2013 8:00 pm
import java.io.*;
import java.util.Vector;
public class VectorToFile {
private Vector victor;
private static final int size = 10;
public VectorToFile () {
victor = new Vector(size);
for (int i = 0; i < size; i++)
victor.addElement(new Integer(i));
}
public void writeList() {
PrintStream out = null;
try {
System.out.println("Start writing to file.");
out = new PrintStream(new FileOutputStream("file.txt"));
for (int i = 0; i < size; i++)
out.println("Value at: " + i + " = " + victor.elementAt(i));
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("ArrayIndexOutOfBoundsException Error:" +
e.getMessage());
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("PrintStream");
out.close();
} else {
System.out.println("Couldn't open connection");
}
}
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.