The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

loading arrays of objects from a binary file

Posted by nbear on May 02, 2001 at 10:22 PM

Is anybody able to help with examples of loading an array of objects from a
binary file.
I have created a class called ShareDetail with instance variables of
private String name;
private int noOfShares;
private int purDate;
private double purPrice;
private double curPrice;

I am now trying to load the array from a binary file as follows and I don't
know what I doing wrong?
import java.io.*;

public class Portfolio
{
public static void main(String[] args) throws IOException
{
ShareDetail[] shareList = new ShareDetails[10];
menuChoice();
mySwitch(menuChoice);
}

file://reads all share details from file
public static int loadShareFile(ShareDetail[] shareList) throws IOException
{
try
{
File PortFile = new File("Portfolio.dat");
DataInputStream inStream = new DataInputStream(new
FileInputStream(PortFile));

ShareDetail aShareDetail;
int index = 0;
boolean moreShares = true;

try
{
while(moreShares)
{
aShareDetail = new ShareDetail();
moreShares = aShareDetail.readShare(inStream);
if(moreShares)
shareList[index] = aShareDetail;
index++;
}
}
catch (EOFException e)
{
}
finally
{
inStream.close();
return index;
}
}
catch (FileNotFoundException e)
{
System.out.println("IOERROR: File NOT Found: \n");
}
catch (IOException e)
{
System.out.println("IOERROR: \n" + e.getMessage());
}
finally
{
}

}

file://reads the share details
public boolean readShare(DataInputStream inStream) throws IOException
{
String name = "";
int noOfShares = 0;
int purDate = 0;
double purPrice = 0.0;
double curPrice = 0.0;

name = inStream.readUTF();
noOfShares = inStream.int(); I am also getting an error saying
that an Identifier was expected here??????????????????
purDate = inStream.int();
purPrice = inStream.double();
curPrice = inStream.double();
}

Any help would be appreciated
Narelle





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us