• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I read from a Vector and writer into a File..

 
Nasser Aboobaker
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I have a function which returns a Vector. I want to read from that Vector and write into a file. Can I do it?. If yes, can you just explain which reader I should use to read from Vector.
Thanks in advance.
Nasser
 
Trefor Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
}
 
Nasser Aboobaker
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trefor,
Thanks for your help.
I'll try that code and if I need any more help I'll get back..
Once again thanks..
Nasser
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goodness, what a big program for a simple task! Surely ObjectOutputStream is the thing to use.
If you construct an ObjectOutputStream, <code>ooStream</code>, from a FileOutputStream, you can write a Vector to it with the single operation:
<code>ooStream.writeObject(myVector);</code>
That will write it in the Java serialisation format, which is not human-readable, but is easily read back into a Java program with readObject().
If you want to write the contents of the Vector as human-readable Strings, you can iterate through the Vector (use the Vector's iterator() method), convert each element to a String, with Object's toString() method, and write each String to your file with ObjectOutputStream's writeUTF() method. The contents of the file will be human-readable if the elements of the Vector are of classes with appropriate overrides of toString().
Reading the human-readable file back into Java could be more difficult. Depends if the objects in the Vector can be constructed from their String representation.
 
Trefor Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-)
Erm, it's not that big a program. I must be honest and say that I did not know about the objectoutputstream class so thanks for correcting me on that one. I don't personally have time to memorise every last bit of the API so I tend to code my own solutions to problems instead.
I do think my class represents a very clean and tidy way of doing things though. It is human readable and very easy to read back into Java.
So there! :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!