23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 13.2. Motif standard Open dialog<br />

<strong>Java</strong> I/O<br />

FileDialog is a subclass of java.awt.Dialog that represents the native save and open<br />

dialog boxes:<br />

public class FileDialog extends Dialog<br />

A file dialog is almost completely implemented by a native peer. Your program doesn't add<br />

components to a file dialog or handle user interaction with event listeners. It just displays the<br />

dialog and retrieves the name and directory of the file the user chose after the dialog is<br />

dismissed.<br />

Since applets normally can't read or write files, file dialogs are primarily useful only in<br />

applications. Nonetheless, there is no specific security manager check to see whether file<br />

dialogs are allowed. Sun's applet viewer, Hot<strong>Java</strong>, and some recent versions of Netscape<br />

Navigator do allow untrusted applets to display file dialogs, retrieve the name and path of the<br />

file selected, and send that information back to the originating host over the network.<br />

Although this is a very minor security hole, since it only exposes the name and path of a<br />

single file selected by the user, it's still on the worrisome side for the paranoid. Internet<br />

Explorer 4.0 and Navigator 4.0.3 and earlier do not allow applets to display file dialogs.<br />

Certainly, you can't count on being allowed to use a file dialog in an applet, nor can you be<br />

guaranteed that it isn't allowed either.<br />

To ask the user to select a file from a file dialog, perform these four steps:<br />

1. Construct a FileDialog object.<br />

2. Set the default directory or file for the dialog (optional).<br />

3. Make the dialog visible.<br />

4. Get the name and directory of the file the user chose.<br />

There is one FileDialog constructor:<br />

public FileDialog(Frame parent, String title, int mode)<br />

The first argument is the parent frame of this file dialog. This will normally be the main<br />

window of the application, the applet's parent, or the frontmost window of the application.<br />

Conversely, you can just create a new frame; you're not required to show the frame if you<br />

307

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!