Java Reference
In-Depth Information
To add this to our code, we will start with a private method that we can use to find out where
the user specified the files should go. This code looks like:
private String getFileRoot(){
String fileRoot = System.getProperty("Statistics.fileRoot");
if (fileRoot == null){
fileRoot = System.getProperty("user.dir");
fileRoot = fileRoot + System.getProperty("file.separator");
return fileRoot;
This code simply looks for the system property named Statistics.fileRoot . If there is no
such property, the call will return null; in that case we use the property named by user.dir .
We end up adding a file separator to the end, and return that to the caller as a string that is the
directory in the filesystem where the directories for the various teams will be located.
Now all we need to do is add this prefix to the methods where we read or write the Play-
erImpl data. This is just another string concatenation, so our constructor and writeState()
methods now look like:
public PlayerImpl(int playerId, String teamName) {
id = playerId;
try {
Integer Id = new Integer(playerId);
ObjectInputStream readIn =
new ObjectInputStream(new FileInputStream(
getFileRoot() + teamName +
System.getProperty("file.separator") + Id.toString()));
PlayerImpl copy = (PlayerImpl) readIn.readObject();
id = playerId;
name =;
team =;
pos = copy.pos;
} catch (IOException e) {
"unable to open file for player, creating new player object");
name = "unknown";
} catch (ClassNotFoundException e) {
System.out.println("unable to read file for player");
Search WWH ::

Custom Search