Java Reference
In-Depth Information
fileName = name;
}
public byte readOneByte()
{
FileInputStream file = null;
try
{
System.out.println(“Opening file for reading...”);
file = new FileInputStream(fileName);
}catch(FileNotFoundException f)
{
System.out.println(“** Could not find “
+ fileName + “ **”);
f.printStackTrace();
return -1;
}
System.out.println(“Just opened file: “ + fileName);
byte x = -1;
try
{
System.out.println(“Reading one byte from file...”);
x = (byte) file.read();
}catch(IOException i)
{
System.out.println(“** Error reading one byte **”);
i.printStackTrace();
return -1;
}
System.out.println(“Just read “ + x);
return x;
}
}
The following CatchDemo program uses a filename input from the com-
mand line. The output in Figure 11.2 shows what occurs when the filename
does not exist and the readOneByte() method attempts to open this nonexis-
tent file.
public class CatchDemo
{
public static void main(String [] args)
{
System.out.println(“Instantiating a
MyFileUtilities object...”);
MyFileUtilities util = new MyFileUtilities(args[0]);
System.out.println(“Invoking readOneByte() method...”);
System.out.println(util.readOneByte());
}
}
Search WWH ::




Custom Search