Not as easy as it may seem. As far as I know, the only way to get to a file is by writing a byte array. Try the following class which accepts a vector and a filename as arguments:
/* This class takes a Vector and a filename. The vector is converted to a byte array and then written to the file indicated by the filename */
import java.io.*;
import java.util.*;
class WriteToFile {
public void WriteToFile (final File f, final Vector v){
counter = 0;
int size = v.size();
String stringsToWrite [] = new String[size];
// First, let's have an array of strings
for(int x=0;x<size;x++){>
Object vecElement = v.elementAt(x);
String stringToConvert = vecElement.toString();
stringsToWrite[x] = stringToConvert;
}
// Now turn it into an array of chars(!)
while (counter<size){>
String s = stringsToWrite[counter];
int tempCharArrayLength = s.length();
char tempCharArray [] = new char tempCharArrayLength];
tempCharArray = s.toCharArray();
for(int y=0;y<tempCharArrayLength;y++){>
char toFinalArray = tempCharArray[y];
finalArray[finalArrayIndex] = toFinalArray;
finalArrayIndex++;
if (finalArrayIndex == finalArray.length-1) {
getBiggerArray(finalArray);
}
}
counter++;
}
// Now write the resulting char array to a file.
// First trim the null elements.
char trimNull[] = new char[finalArrayIndex];
System.arraycopy(finalArray, 0, trimNull, 0, finalArrayIndex);
finalArray = trimNull;
// Then turn the char array into a byte array
byte toFile [] = new byte[finalArray.length];
for(int z=0;z<finalArray.length;z++){>
toFile[z] =((byte) finalArray[z]);
}
// And then write it to file f
try{
FileOutputStream out = new FileOutputStream(f);
out.write(toFile);
out.close();
}
// Catching any IO Exceptions in a nice friendly way
catch(IOException e){
System.out.println("Couldn't Write the file");
}
}
public void getBiggerArray(char array []){
char copy [] = new char[2*array.length];
System.arraycopy(array, 0, copy, 0, array.length);
finalArray = copy;
}
private int counter, finalArrayIndex;
private char finalArray [] = new char [2];
}