img
// open output file
try {
fout = new FileOutputStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println("Error Opening Output File");
return;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("File Error");
}
fin.close();
fout.close();
}
}
Notice the way that potential I/O errors are handled in this program. Unlike some other
computer languages, including C and C++, which use error codes to report file errors, Java
uses its exception handling mechanism. Not only does this make file handling cleaner, but
it also enables Java to easily differentiate the end-of-file condition from file errors when
input is being performed. In C/C++, many input functions return the same value when
an error occurs and when the end of the file is reached. (That is, in C/C++, an EOF condition
often is mapped to the same value as an input error.) This usually means that the programmer
must include extra program statements to determine which event actually occurred. In Java,
errors are passed to your program via exceptions, not by values returned by read( ).
Thus, when read( ) returns 1, it means only one thing: the end of the file has been
­
encountered.
Applet Fundamentals
All of the preceding examples in this topic have been Java console-based applications. However,
these types of applications constitute only one class of Java programs. Another type of program
is the applet. As mentioned in Chapter 1, applets are small applications that are accessed on an
Internet server, transported over the Internet, automatically installed, and run as part of a web
document. After an applet arrives on the client, it has limited access to resources so that it
can produce a graphical user interface and run complex computations without introducing
the risk of viruses or breaching data integrity.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home