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.

}<br />

}<br />

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

if (result == JFileChooser.APPROVE_OPT<strong>IO</strong>N) {<br />

try {<br />

File f = fc.getSelectedFile();<br />

if (f != null) { // Make sure the user didn't choose a directory.<br />

FileInputStream fin = new FileInputStream(f);<br />

StreamCopier.copy(fin, System.out);<br />

fin.close();<br />

}<br />

}<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

}<br />

// Work around annoying AWT non-daemon thread bug.<br />

System.exit(0);<br />

This program is shorter and simpler than Example 13.1. It's also more reliable across different<br />

platforms. The only downside is that it's noticeably slower to start up. However, that's because<br />

it must load the entire Swing package. A program that's using many Swing classes won't have<br />

to pay that penalty more than once. The only other disadvantage is that standard file extenders<br />

like Action Files or Super Boomerang won't notice a file chooser, whereas they will work<br />

inside a file dialog brought up by a <strong>Java</strong> program.<br />

13.2.4 Programmatically Manipulating the JFileChooser<br />

The JFileChooser class includes several methods to specify which files and directories are<br />

selected and displayed when the chooser is shown. These include:<br />

public void changeToParentDirectory()<br />

public void rescanCurrentDirectory()<br />

public void ensureFileIsVisible(File f)<br />

The changeToParentDirectory() method simply displays the parent directory of the<br />

directory currently displayed; that is, it moves one level up in the directory hierarchy. The<br />

rescanCurrentDirectory() method refreshes the list of files shown. Use it when you have<br />

reason to believe a file may have been added to or deleted from the directory.<br />

ensureFileIsVisible() scrolls the list up or down until the specified file is shown.<br />

Three methods allow you to specify which directory and file are selected in the file chooser:<br />

public void setSelectedFile(File selectedFile)<br />

public void setSelectedFiles(File[] selectedFiles)<br />

public void setCurrentDirectory(File dir)<br />

You can use these to point the user at a particular file. For instance, a <strong>Java</strong> source code editor<br />

might like to set the filename to the title of the class being edited plus the customary .java<br />

extension. Another common example: if the user opens a file, edits it, then selects Save As...<br />

from the File menu, it's customary to bring up the save dialog with the previous location of<br />

the file already selected. The user can change this if they like.<br />

316

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

Saved successfully!

Ooh no, something went wrong!