User`s manual

5 Calling Java from MATLAB
5-74
Description of Function phonebook
The major tasks performed by phonebook are:
1. Determine the Data Directory and Full Filename
The first statement assigns the phone book filename, 'myphonebook', to the
variable
pbname. If the phonebook program is running on a PC, it calls the
java.lang.System static method getProperty to find the directory to use for
the data dictionary. This will be set to the user’s current working directory.
Otherwise, it uses MATLAB function
getenv to determine the directory, using
the system variable
HOME which you can define beforehand to anything you
like. It then assigns to
pbname the full pathname, consisting of the data
directory and filename '
myphonebook'.
function phonebook(varargin)
pbname = 'myphonebook'; % name of data dictionary
if ispc
datadir = char(java.lang.System.getProperty('user.dir'));
else
datadir = getenv('HOME');
end;
pbname = fullfile(datadir, pbname);
2. If Needed, Create a File Output Stream
If the phonebook file does not already exist, phonebook asks the user whether
to create a new one. If the user answers
y, phonebook creates a new phone book
by constructing a
FileOutputStream object. In the try clause of a try-catch
block, the argument
pbname passed to the FileOutputStream constructor is the
full name of the file that the constructor creates and opens. The next statement
closes the file by calling
close on the FileOutputStream object FOS. If the
output stream constructor fails, the
catch statement prints a message and
terminates the program.
if ~exist(pbname)
disp(sprintf('Data file %s does not exist.', pbname));
r = input('Create a new phone book (y/n)?','s');