Reading / Writing a VECTOR to diskfile...
I simply can NOT get this to work...
Any ideas?
Write the Vector to disk
FileOutputStream fos =new FileOutputStream(diskFile);
ObjectOutputStream oos =new ObjectOutputStream(fos);
oos.writeObject(personVector);
oos.close();
(I print the file before writing - system.out.print(personVector);, and it looks fine...
Read the file from disk, and cast to VECTOR
ObjectInputStream ois =new ObjectInputStream(new FileInputStream(diskFile));
personVector =new Vector();
personVector = (Vector)ois.readObject();
ois.close();
I print the file after reading, and only get partial info (the extended parts of the original 'Person' class called PersonalFriend or BusinessAssociate), and it doesn't cast it into the Vector at all..
Any help would be greatly appreciated.
[1023 byte] By [
dewlovera] at [2007-11-26 21:07:48]

> I print the file after reading, and only get partial info (the extended parts of the
> original 'Person' class called PersonalFriend or BusinessAssociate), and it doesn't
> cast it into the Vector at all..
I'm not sure I understand this. Perhaps you could post some (brief) code that
illustrates your problem - something that writes then reads a Vector.
The following - using the two code extracts you provided does what it should:import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class Person implements Serializable {
private String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return "Person: " + name;
}
private static final String FILE_NAME = "data.dat";
/**
* @param args
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
Vector testData = new Vector();
testData.add(new Person("Arthur"));
testData.add(new Person("Bob"));
testData.add(new Person("Charlie"));
System.out.println(testData);
writeData(testData);
testData = readData();
System.out.println(testData);
}
private static void writeData(Vector personVector) throws IOException {
File diskFile = new File(FILE_NAME);
FileOutputStream fos = new FileOutputStream(diskFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(personVector);
oos.close();
}
private static Vector readData() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
//personVector = new Vector();
Vector personVector = (Vector)ois.readObject();
ois.close();
return personVector;
}
}
The output is:[Person: Arthur, Person: Bob, Person: Charlie]
[Person: Arthur, Person: Bob, Person: Charlie]
Here's what I have...
WRITE the VECTOR to a disk
try {
File diskFile = new File(pathName);
FileOutputStream fos = new FileOutputStream(diskFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(personVector);
oos.close();
} //Ends TRY
catch (
The diskFile name is A:\address.dat and is input from the user
-
When I try to READ the same VECTOR that I just wrote...
if (diskFile.isFile()) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(diskFile));
Vector personVector = (Vector) ois.readObject();
ois.close();
} //Ends TRY
catch (
At this point, I try to list any of the entries in the Vector, and the Vector comes back EMPTY, so is therefore NOT being cast properly...
I appreciate the help greatly. I've tried implementing your code, but it STILL is not showing anything in the Vector...
Any more ideas? I only have 3 more strands of hair left to pull out...
> the Vector comes back EMPTY, so is therefore NOT being cast properly...
On the contrary, if the Vector comes back empty it is being cast properly: since
only vectors (and not objects generally) are capable of being empty.
The question is, therefore, why is it empty?
All sorts of possibilities suggest themselves: one or other of the catch blocks silently
cuttting the write or read process short before it has finished, one file being written
and another being read, the vector being tested for emptiness (or printed or
whatever) not being the same s the vector that was read, ... Who shall say? The
list of possible problems is endless.
That's why I suggested you post some code that illustrates the problem. Something
brief that compiles and runs and shows the problem you are having. The tricky bit
(and it is tricky) is coming up with something that is brief and self contained, yet
still shows the problem. Often the effort spent on constructing such an example is
rewarded with a solution suggesting itself as all the distractions are cleared away.
> I've tried implementing your code, but it STILL is not showing anything in the
> Vector...
My code gives the output that I said it gives. I only really posted it to show that what
you had posted was not really the problem. The problem lies somewhere else;
somewhere in the code you have not posted.
(By the way, you declare personVector inside the try block which is kind of
weird if you ever intend doing anything with it, even printing it. It suggests we are
still seeing a rather edited version of the code.)
Here is the VECTOR befor I write it...
Entry number: 1
Personal Friend:
Last modified on: Fri Mar 09 23:41:39 PST 2007
Name: Jimmy Hoffa
Address: end zone
City: stadium
State: NY
Zip Code: 12345
Phone: 800-555-1212
Birth Date: 01/02/1933
E-mail address: jimmyh@comcast.net
I write it to disk OK, then when I read it back, I get this...
[
Personal Friend:
Last modified on:
Name:
Address:
City:
State:
Zip Code:
Phone:
Birth Date: 01/02/1933
E-mail address: jimmyh@comcast.net]
As you can see, a LOT of data is missing...
Any ideas WHY the Vector is NOT being populated? Could it have anything to do with my extensions of the Person class?
Here is EXACTLY what I have written...
private static void downloadBook(Vector personVector) throws IOException{
String pathName = "A:\\"; //Set the DEFAULT path
final char delimiter = File.separatorChar;
System.out.print("\n \t [* System-dependent path seperator character is " + delimiter + " *] \n");
System.out.print("\n \t Please enter the FILENAME to be saved: -->>");
String inputFileName = MyInput.readString(); //Get the FILE NAME from the user
inputFileName = inputFileName.trim(); //Trim off any leading or trailing spaces
//Create the FILENAME using the DEFAULT and USER INPUT (if any was made)
if (inputFileName != "") {
pathName += inputFileName;
}
try {
File diskFile = new File(pathName);
FileOutputStream fos = new FileOutputStream(diskFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(personVector);
oos.close();
} //Ends TRY
catch (Exception e) {
e.printStackTrace();
} //Ends CATCH
} //Ends DOWNLOADBOOK method
private static void uploadBook() throws IOException, ClassNotFoundException {
Vector personVector = new Vector();
String pathName = "A:\\"; //Set the DEFAULT path
final char delimiter = File.separatorChar;
System.out.print("\n \t [* System-dependent path seperator character is " + delimiter + " *] \n");
System.out.print("\n \t Please enter the subdirectory or filename (or hit Enter for default): -->>");
String inputFileName = MyInput.readString(); //Get the FILE NAME from the user
inputFileName = inputFileName.trim(); //Trim off any leading or trailing spaces
//Create the FILENAME using the DEFAULT and USER INPUT (if any was made)
pathName += inputFileName;
final File diskFile = new File(pathName); //Create an instance of the FILE class using the user input
if (!diskFile.exists()) {
System.out.print("\n \t " + pathName + " does not exist... \n \t Please try again!");
}
else {
if (diskFile.isDirectory()) {
System.out.print("\n \t " + pathName + " is the current directory.");
String[] contents = diskFile.list();
if (contents == null) {
System.out.print("\n \t and it is EMPTY!");
}
else { //If the directory is there, and not empty, display files in columns
System.out.print("\n \t There are " + contents.length + " files present: \n ");
for (int counter = 0; counter < contents.length; counter++) {
System.out.print("\n \t " + contents[counter]);
if ((counter + 1) % 3 == 0)
System.out.println("");
} //Ends the FOR loop
System.out.println("");
} //Ends the CONTENTS NULL 'if' statement
} //... IS IT A DIRECTORY 'if' statement
else { //Not a DIRECTORY
if (diskFile.isFile()) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(diskFile));
personVector = (Vector)ois.readObject();
ois.close();
System.out.print(personVector);
} //Ends TRY
catch (Exception e) {
e.printStackTrace();
} //Ends CATCH
} //Ends ISDISKFILE 'if' statement
else { //Not a file - display error message
System.out.print("\n \t Sorry, file name entered is not a valid");
System.out.print("\n \t file in the directory " + pathName);
} //Ends the ISFILE 'if' statement
} //Ends the NOT DIRECTORY else
} //Ends the FILE EXISTS 'if' statement
} //Ends UPLOADBOOK method
If you see something, I would appreciate a holler back...
If you need me, I will be out in the back yard eating worms...
:-(
